gpt4 book ai didi

firebase - flutter firebase 从 Firestore 获取数组并将其分配给列表

转载 作者:行者123 更新时间:2023-12-04 09:21:49 27 4
gpt4 key购买 nike

我在 Firestore 有一个收藏。它有一个字段数组,我试图从 Firestore 获取该数组并将其分配给 flutter 中的列表。
我的收藏如下
enter image description here
我从 Firestore 获取数据的代码

List<Offset> pointlist = <Offset>[];

getdata() async{
await Firestore.instance.collection("points").document('biZV7cepFJA8T6FTcF08').get().then((value){
setState(() {

List<Offset> pointlist = List.from(value.data['point']);

});
});
}

@override
void initState() {
super.initState();
getdata();
}
我得到这个错误类型“字符串”不是“偏移”类型的子类型

最佳答案

你做错的事情是这样的:

// You have initialised your List as a Offset Object Type
List<Offset> pointList;

Secondly, the data you are assigning is a String, if you closely take a look at that firebase.

"Offset(x,y)"

Finally, trying to assign the String value to a List of type Offset class/object


If you want to make the thing works, then either make the List of type String and then add it to the List

List<String> pointlist = List.from(value.data['point']); 

Or first Add the data to the Offset Object like this, and then pass it to the List

List<Offset> pointList = <Offset>[];

getdata() async{
await Firestore.instance.collection("points").document('biZV7cepFJA8T6FTcF08').get().then((value){
setState(() {
// first add the data to the Offset object
List.from(value.data['point']).forEach((element){
Offset data = new Offset(element);

//then add the data to the List<Offset>, now we have a type Offset
pointList.add(data);
});

});
});
}
总结
始终寻找您提供给 List 的数据类型, 如果您尝试添加不是类型的数据 TList<T> ,您将始终收到此类型不匹配的错误。希望这会给你一些关于编程的清晰和一些基本的想法。保持学习 :)

关于firebase - flutter firebase 从 Firestore 获取数组并将其分配给列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63091889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com