firebase - 在 null 上调用了 getter 'image'
问题描述
我正在尝试从云火存储中获取 url 列表并使用该从火基础存储中获取图像。
图像按预期加载,但控制台显示一些错误,如下所示。
I/flutter (24353): The following NoSuchMethodError was thrown during a
scheduler callback:
I/flutter (24353): The getter 'image' was called on null.
I/flutter (24353): Receiver: null
I/flutter (24353): Tried calling: image
I/flutter (24353): When the exception was thrown, this was the stack:
我试过了。
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('images').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new CircularProgressIndicator();
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Center(child: new CircularProgressIndicator());
default:
return new StaggeredGridView.countBuilder(
padding: const EdgeInsets.all(8.0),
crossAxisCount: 4,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, i) {
String imgPath = snapshot.data.documents[i]['link'];
return new Material(
elevation: 8.0,
borderRadius:
new BorderRadius.all(new Radius.circular(8.0)),
child: new InkWell(
child: new Hero(
tag: imgPath,
child: new FadeInImage(
image: new NetworkImage(imgPath),
fit: BoxFit.cover,
placeholder: new AssetImage("images/images.jpg"),
),
),
),
);
},
staggeredTileBuilder: (i) =>
new StaggeredTile.count(2, i.isEven ? 2 : 3),
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
);
}
},
)
解决方案
如果图像加载正常但如您所提到的有空错误,则第一次运行可能Widget build()
在NetworkImage()
. 一种解决方法是对imgPath
. 仅当图像 URL 不为空时,该placeholder
参数才能正常工作,如此处所示。
FadeInImage(
// if imgPath != null, display Networkimage, else display AssetImage
image: (imgPath != null)? NetworkImage(imgPath) : AssetImage("images/images.jpg"),
fit: BoxFit.cover,
),
这里的另一个选择是使用loadingBuilder
和errorBuilder
构造函数Image.network()
Image.network(
imgPath,
fit: BoxFit.cover,
// display progress indicator
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent loadingProgress) {
if (loadingProgress == null) return child;
return CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes
: null,
);
},
// display error image
errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
debugPrint('Error loading image: $exception \n Stack trace: $stackTrace');
return Image.asset('images/error_loading.png');
},
),
推荐阅读
- r - 在线串几何图形ggplot内填充颜色或将线串转换为多面体
- javascript - Trying to delete an element from an array with objects
- javascript - Expect jasmine Spy to be called "eventually", before timeout
- javascript - How to replace certain words in a variables in JavaScript
- python - why do I get a single field value in Django formset_factory?
- python - PYQT5:桂冻结
- c# - 在 JSON 反序列化中跳过空数组
- android-10.0 - 如何在 Android Q 中创建后台系统服务
- python - 什么是通过第三个列表的索引将列表的值添加到另一个列表的值并输出前两个列表之一的方法?
- python - 合并两个数据框的更有效方法