Flutter嵌套的燃烧基地距离



有人能解释一下为什么这个类运行时productlist变量为null吗?看起来变量是在第一个istance结束后填充的,所以CardShopList小部件没有来自嵌套istance的productlist。你有什么建议吗?谢谢

Future getMyShopLists(User user) async {
List<Widget> listItems = [];
FirebaseFirestore.instance
.collection('user')
.doc(user.uid)
.collection('shoplist')
.get()
.then((event) {
event.docs.forEach((shoplist) {
List<ProductModel> productlist = [];
Map shopListData = shoplist.data();
shoplist.reference.collection('productlist').get().then((element) {
Map productData = shoplist.data();
element.docs.forEach((doc) {
productlist.add(ProductModel(
id: doc.id,
name: productData['name'],
quantity: productData['quantity'],
price: productData['price'],
));
});
});
listItems.add(CardShopList(
shoplist.id,
shopListData['name'],
productlist, // <------------------------------- THIS IS NULL!!!!
getMyShopLists,
key: ValueKey<String>(shoplist.id),
));
});
if (this.mounted) {
setState(() {
shopListsWidget = listItems;
});
}
});
return shopListsWidget;
}

EDIT我在这个FutureBuilder:中使用该类

FutureBuilder(
future:
searchIcon.icon == Icons.search ? getMyShopLists(user) : null,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return isShopListEmpty
? new Center(
child: new Text(
"x",
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
)
: Container(
child: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: shopListsWidget.length,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
return shopListsWidget[index];
}))
],
),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),

;产品列表";由于您正在使用foreach,因此不应填充。可能有其他解决方案,但将foreach转为for可能会解决这个问题

...
await FirebaseFirestore.instance //added await
.collection('user')
.doc(user.uid)
.collection('shoplist')
.get()
.then((event) async{ //added async
for(QueryDocumentSnapshot shoplist in event.docs){ //---this line changed
List<ProductModel> productlist = [];
Map shopListData = shoplist.data();
await shoplist.reference.collection('productlist').get().then((element) //added await 
{
Map productData = shoplist.data();
for(QueryDocumentSnapshot doc in element.docs) //---this line changed
{
productlist.add(ProductModel(
id: doc.id,
name: productData['name'],
quantity: productData['quantity'],
price: productData['price'],
));
}       
});
listItems.add(CardShopList(
shoplist.id,
shopListData['name'],
productlist,
getMyShopLists,
key: ValueKey<String>(shoplist.id),
));
}
...

请记住,我没有测试过这个,但我以前有过这个问题。

我的建议是移动这个代码块:

listItems.add(CardShopList(
shoplist.id,
shopListData['name'],
productlist, // <------------------------------- THIS IS NULL!!!!
getMyShopLists,
key: ValueKey<String>(shoplist.id),
));

在firebase内部查询如下:

shoplist.reference.collection('productlist').get().then((element) {
Map productData = shoplist.data();
element.docs.forEach((doc) {
productlist.add(ProductModel(
id: doc.id,
name: productData['name'],
quantity: productData['quantity'],
price: productData['price'],
));
});
listItems.add(CardShopList(
shoplist.id,
shopListData['name'],
productlist,
getMyShopLists,
key: ValueKey<String>(shoplist.id),
));
});

null值的原因是firebase查询有一个局部变量,它们只在请求时存在。使用它们的唯一方法是在查询回调中进行本地处理。

您将其定义为async函数,但没有按顺序等待查询处理,您可以尝试在以下位置添加等待吗:

//here #1
await FirebaseFirestore.instance
.collection('user')
.doc(user.uid)
.collection('shoplist')
.get()
.then((event) async {
event.docs.forEach((shoplist) {
List<ProductModel> productlist = [];
Map shopListData = shoplist.data();
//here#2
await shoplist.reference.collection('productlist').get().then((element) {
Map productData = shoplist.data();
element.docs.forEach((doc) {
productlist.add(ProductModel(
id: doc.id,
name: productData['name'],
quantity: productData['quantity'],
price: productData['price'],
));
});
});

最新更新