firebase - 输入'未来' 不是类型 'List 的子类型' 在类型转换中
问题描述
List<Profile> demoProfiles = getdata() as List<Profile>;
Future getdata() async{
List<Profile> list=[];
list = await Firestore.instance
.collection("users").getDocuments().then((querySnapshot){
querySnapshot.documents.forEach((document) {
list.add( Profile(
photos: [document['photoUrl'],],
name: document['nickname'],
age: 2,
distance: 2,
education: document['photoUrl']
));
});
return list;
});
}
class _MainControllerState extends State<MainController> {
final MatchEngine matchEngine = MatchEngine (
matches:demoProfiles.map((Profile profile) => Match(profile: profile)).toList()
);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 30.0),
child: CardStack (
matchEngine: matchEngine
),
);
}
}
当我运行我的代码时,它返回错误:type 'Future' is not a subtype of type 'List' in type cast。我该如何解决?在我从 Firebase 获取数据之前,它是 matchEngine 运行的吗?我尝试将 await 添加到 List demoProfiles = getdata() as await List; 但它返回错误。我对颤振真的很陌生,这个问题整天都在困扰着我。请帮我
解决方案
创建一个FutureBuilder
小部件,并将方法分配给getdata()
属性future
。
Future getdata() async{
List<Profile> list=[];
list = await Firestore.instance
.collection("users").getDocuments().then((querySnapshot){
querySnapshot.documents.forEach((document) {
list.add( Profile(
photos: [document['photoUrl'],],
name: document['nickname'],
age: 2,
distance: 2,
education: document['photoUrl']
));
});
return list;
});
}
FutureBuilder(
future: getdata(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
//...
推荐阅读
- sharepoint-online - 自定义 Sharepoint Online 下拉列表内容
- c# - 无法设置 DataGrid 虚拟化模式
- python - 当原始列表不会更改时,列表切片与使用指针
- ssl-certificate - 使用单独的 SSL 证书绑定多个站点
- python - 在 Python 中随机选择一个列表?
- javascript - Vue生成路由器链接
- python - 尝试按函数订购 Flask SQLAlchemy 对象
- react-native - 如何在 React Native 视频中显示和隐藏字幕
- ruby - 整数的递归序列
- c++ - 类初始化似乎被误认为是函数声明