代码之家  ›  专栏  ›  技术社区  ›  Loolooii

List<dynamic>不是List<Option>

  •  0
  • Loolooii  · 技术社区  · 7 年前

    我有一个云Firebase数据库 questions 收集每个 question 他有一张地图清单 options . 我正在使用flifter,并为其提供以下课程: 问题 option :

    class Question {
      final String text;
      final List<Option> options; // I have tried changing this to List<dynamic> but it doesn't help
      final String reference;
    
      Question(this.text, this.options, this.reference);
    
      Question.fromMap(Map<String, dynamic> map, {this.reference}) : 
        text = map['text'],
        options = map['options']; // here the error happens
    
      Question.fromSnapshot(DocumentSnapshot snapshot)
         : this.fromMap(snapshot.data, reference: snapshot.documentID);
    }
    

    选项

    class Option {
      final String text;
      final int votes;
      bool isSelected;
    
      Option(this.text, this.votes, this.isSelected);
    
      Option.fromMap(Map<String, dynamic> map) : 
        text = map['text'],
        votes = map['votes'];
    }
    

    我真的不知道该怎么解决这个问题。然而,这种错误似乎在互联网上随处可见。感谢您的帮助。

    使现代化

    供您参考: 选项 是云端Firestore中的一组地图。

    我根据以下两个答案将代码更改为:

    factory Question.fromMap(Map<String, dynamic> map) {
    
        var options = map['options'];
        var text = map['text'];
        var reference = map['documentID'];
        List<Option> optionsList = options.cast<Option>();
    
        return new Question(
          text = text,
          options = optionsList,
          reference = reference
        );
      }
    
      factory Question.fromSnapshot(DocumentSnapshot snapshot) {
        return Question.fromMap(snapshot.data);
      }
    

    尽管如此,我还是发现了这个错误: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Option' in type cast

    我已经看到并尝试过很多答案,而且似乎都说了同样的话。我就是搞不懂。

    Cloud Firestore的屏幕截图:

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  8
  •   cipli onat    7 年前

    我以前遇到过这个问题,我用

    这是我的代码,你可以把它转换成你的:

    UserModel.fromSnapshot(DocumentSnapshot snapshot)
          : documentReference = snapshot.reference,
            username = snapshot['username'],
            firstName = snapshot['firstName'],
            lastName = snapshot['lastName'],
            picture = snapshot['picture'],
            routes = Map.from(snapshot['routes']),
            avgScore = snapshot['avgScore'];
    }
    

    你在cloud_firestore上的字段类型是什么?

    如果是 Map 尝试使用 Map.from()

    如果是 List 尝试使用 List.from()

    并将本地字段更改为相同的类型。

        2
  •  2
  •   Chiziaruhoma Ogbonda    6 年前

    在遇到类似问题后,这对我来说非常有效:

    class Question {
      final String text;
      final List<Option> options = [];
      final String reference;
    
      Question(this.text, this.options, this.reference);
    
      Question.fromMap(Map<String, dynamic> map, {this.reference}){
        text = map['text'];
        if (map['options'] != null) {
          map['options'].forEach((v) {
          options.add(new Option.fromMap(v));
        });
       }
      }
    
      Question.fromSnapshot(DocumentSnapshot snapshot)
        : this.fromMap(snapshot.data, reference: snapshot.documentID);
    }
    
    class Option {
      final String text;
      final int votes;
      bool isSelected;
    
      Option(this.text, this.votes, this.isSelected);
    
      Option.fromMap(Map<String, dynamic> map) : 
        text = map['text'],
        votes = map['votes'];
    }
    
        3
  •  1
  •   function1983    7 年前

    此代码比建议的解决方案更具可读性。我在我的项目中使用了它。

    var optionsMap = map['options'] as List;
    List<Option> optionsList = optionsMap.map((o) => Option.fromMap(o.cast<String, dynamic>())).toList();
    
    class Option {
      final String text;
      final int votes;
      bool isSelected;
    
      Option(this.text, this.votes, this.isSelected);
    
      Option.fromMap(Map<dynamic, dynamic> map) : // notice Map<dynamic, dynamic> here
        text = map['text'],
        votes = map['votes'];
    }
    

    How to fix this Error: '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

        4
  •  0
  •   harsh    7 年前

    我们正在请求一个列表,但是我们得到了一个列表,因为我们的应用程序还不能识别类型。

    为了消除这个错误,我们必须显式地强制转换动态值。 代替

      Question.fromMap(Map<String, dynamic> map, {this.reference}) : 
      text = map['text'],
      options = map['options']; 
    

    使用

      factory Question.fromMap(Map<String, dynamic> map, {this.reference}) {
       var optionsFromMap  = map['options'];
    
      //you can use both the steps to check which one will work perfectly for you
      // List<Option> optionsList = new List<Option>.from(optionsFromMap)
       List<Option> optionsList = optionsFromMap.cast<Option>();
    
      return new Question(
        text = map['text'],
        options = map['options']; 
      );
    }
    

    当我想要列表值时,上面的代码工作得很好

    我希望这对你有帮助。

        5
  •  0
  •   Loolooii    7 年前

    最后我跟着 @chipli onat 建议并将其与 @harsh 回答并最终使其起作用。这就是我的代码现在的样子:

    class Question {
      final String text;
      final String reference;
      final List<Map> options;
    
      Question(this.text, this.options, this.reference);
    
      factory Question.fromMap(Map<String, dynamic> map, String reference) {
    
        var options = map['options'];
        var text = map['text'];
        List<Map> optionsList = options.cast<Map>();
    
        return new Question(
          text = text,
          options = optionsList,
          reference = reference
        );
      }
    
      factory Question.fromSnapshot(DocumentSnapshot snapshot) {
        return Question.fromMap(snapshot.data, snapshot.documentID);
      }
    }
    

    class Option {
      final String text;
      final int votes;
      bool isSelected;
    
      Option(this.text, this.votes, this.isSelected);
    
      Option.fromMap(Map<dynamic, dynamic> map) : // notice Map<dynamic, dynamic> here
        text = map['text'],
        votes = map['votes'];
    }
    

    我相信这个答案是可以改进的,但说实话,我不完全理解为什么这么难。我希望这个答案对你有帮助。

        6
  •  0
  •   durje    4 年前

    这对我很有用:

     Question.fromMap(Map<dynamic, dynamic> map, {this.reference}) : 
        text = map['text'],
        options = map['options'].map<Option>((option) {
              return Option.fromMap(option);
            }).toList();