且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

列出< dynamic>不是List< Option>的子类型

更新时间:2022-11-30 11:02:07

我最终遵循了 @chipli onat 的建议,并将其与 @harsh 答案混合,最后得到了它去工作.这是我的代码现在的样子:

I ended up following @chipli onat advice and mix it with @harsh answer and finally got it to work. This is how my code looks like now:

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'];
}

我敢肯定,这个答案会有所改善,但是老实说,我不完全理解为什么它必须如此困难.希望这个答案对您有所帮助.

I'm sure this answer could be improved, but to be honest I don't understand entirely why it has to be so difficult. I hope this answer helps you.