【发布时间】:2021-05-23 13:58:48
【问题描述】:
大家好,如果我的英语不清楚,首先我很抱歉,我正在做一个个人项目。因此,我在 Firestore 文档上使用 StreamBuilder,其用户 ID 来自“用户”集合。因此,我已检索“imageUrl”字段并将其显示在我的应用程序的图像网络中,因此,我有“删除帐户”按钮,此按钮将从 firebase auth 中删除该帐户,并删除 streambuilder 侦听的文档.
因此,发生错误是因为流构建器将构建 ImageNetwork 并从文档字段中检索 URL。 有什么办法可以处理这个错误吗?
这是将返回 NetworkImage 的 streamBuilder 的代码
StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance
.collection('Users')
.document(user.getID())
.snapshots(),
builder:
(context, AsyncSnapshot<DocumentSnapshot> snapshot) {
print(snapshot.connectionState);
var userDocument = snapshot.data;
if (userDocument.data.length == 0) {
return const Center(
child: Text(
"Not Available",
style:
TextStyle(fontSize: 30.0, color: Colors.grey),
),
);
} else
return AvatarGlow(
glowColor: Colors.redAccent,
endRadius: 90,
child: Material(
elevation: 8.0,
shape: CircleBorder(),
child: CircleAvatar(
backgroundColor: Colors.grey[100],
child: ClipOval(
child: FadeInImage(
image: NetworkImage(
userDocument['imageUrl'] ??
'https://picsum.photos/250?image=9'),
placeholder: AssetImage('assets/noImage.png'),
),
),
radius: 70,
),
),
);
},
),
调试错误
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was
StreamBuilder<DocumentSnapshot>
如果 else 阻塞,解决方案就开启
StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance
.collection('Users')
.document(user.getID())
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.data != null && snapshot.data.exists) {
var userDocument = snapshot.data;
// return something
}
}
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore