列出< dynamic>不是List< Option>的子类型 [英] List<dynamic> is not a subtype of List<Option>
问题描述
我有一个Cloud Firebase数据库,其中包含一个 questions
集合.每个问题
都有一个地图选项
的列表.我正在使用Flutter,并具有以下用于 question
和 option
的类:
I have a Cloud Firebase database with a questions
collection. Each question
has a list of map options
.
I'm using Flutter and have the following classes for question
and 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);
}
和 option
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'];
}
我真的不知道该如何解决.不过,这种错误似乎在互联网上无处不在.任何帮助表示赞赏.
I really have no idea how I can fix this. This kind of error seems to be everywhere on the internet, though. Any help is appreciated.
更新
供您参考: options
是Cloud Firestore中的一组地图.
For your information: options
is an array of map in Cloud Firestore.
我根据以下两个答案将代码更改为:
I changed my code based on the two answers below to:
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>'不是强制类型转换中选项"类型的子类型
我已经看到并尝试了很多答案,而且所有人似乎都说相同的话.我只是想不通.
I have seen and tried so many answers and all seem to say the same. I just can't figure this out.
Cloud Firestore的屏幕截图:
Screenshot of Cloud Firestore:
推荐答案
我最终遵循了 @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.
这篇关于列出< dynamic>不是List< Option>的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!