firebase - 带有列表的 Flutter Futurebuilder 不起作用
问题描述
我有一个带有 Future 函数的 Futurebuilder,它返回一个包含来自 Firebase 存储的图像 url 的列表。
child: FutureBuilder(
future: getImages(),
builder: (context, AsyncSnapshot snapshot) {
//print(snapshot.data);
if (snapshot.connectionState == ConnectionState.done) {
print(snapshot.data[0]);
return ListView.builder(
shrinkWrap: true,
itemCount: 2,//snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
//var downloadUrl = snapshot.data[index];
return ListTile(
contentPadding: EdgeInsets.all(8.0),
leading: Image.network(
//downloadUrl
"https://placehold.it/500x500"
, fit: BoxFit.fill),
);
});
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
return CircularProgressIndicator();
},
),
Future<List<String>> getImages() async{
var showcaseCode = await page.where("userID",isEqualTo:
FirebaseAuth.instance.currentUser.uid).get();
List downloadUrlList = [];
var showcaseCodeLength = showcaseCode.docs.length;
for(var i = 0; i < showcaseCodeLength; i++){
var url = await FirebaseStorage.instance.ref()
.child("showcase_images")
.child(showcaseCode.docs[i]["showcaseCode"])
.getDownloadURL();
await downloadUrlList.add(url);
}
print(downloadUrlList);
return downloadUrlList;
}
当我downloadUrlList
在getImages
未来打印时,我的 url 列表有正确的输出。但如果我downloadUrlList
在 Futurebuilder 中打印,我会收到以下错误:
The method '[]' was called on null.
Receiver: null
Tried calling: [](0)
谢谢你的帮助!
解决方案
要在这个问题上有答案:
正如@pskink 评论,使用以下代码解决了这个问题:
List downloadUrlList = <String>[]
或者
List<String> downloadUrlList = []
推荐阅读
- sql - 在sql中将日期范围拆分为周
- pandas - 如何标准化DataFrame中的每组?
- python - 如何在 python 程序中更改系统时区?
- python - 熊猫在 GroupBy 之后保留其他列 - 不是总和
- r - 错误:使用 arma::dot 时未定义对“sdot_”的引用
- java - 如何在 dropwizard 身份验证方法中创建 GET 请求?
- python - 使用 ffmpeg 连接视频写入空音频流
- java - Gson:如何为链接对象编写反序列化器
- numba - 如何在 Numba 中创建类型化的字典 where (key, value) = (str, list)
- pine-script - 查找本周内的第一个柱