Android 错误状态:无法在DocumentSnapshot平台上获取不存在的字段,Firebase Flatter
我正在按文档id获取数据,但出现以下错误: 错误状态:无法在DocumentSnapshot平台上获取不存在的字段 它正在工作,我可以通过文档id从firebase获取数据,但它在调试控制台中给出了错误 我正在使用StreamBuilder获取数据:Android 错误状态:无法在DocumentSnapshot平台上获取不存在的字段,Firebase Flatter,android,firebase,flutter,dart,google-cloud-firestore,Android,Firebase,Flutter,Dart,Google Cloud Firestore,我正在按文档id获取数据,但出现以下错误: 错误状态:无法在DocumentSnapshot平台上获取不存在的字段 它正在工作,我可以通过文档id从firebase获取数据,但它在调试控制台中给出了错误 我正在使用StreamBuilder获取数据: StreamBuilder( stream: _databaseService.productCollection.doc(docID).snapshots(), builder: (context, sn
StreamBuilder(
stream: _databaseService.productCollection.doc(docID).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
valueColor:
new AlwaysStoppedAnimation<Color>(Colorsx.mainColor),
),
);
}
var document1 = snapshot.data;
return Container(
decoration: BoxDecoration(
color: Colorsx.mainColor,
borderRadius: radius,
),
// color: Colorsx.mainColor,
child: Column(
children: [
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Chip(
label: LabelText1("Ürün Adı: "),
backgroundColor: Colorsx.mainColor,
),
Chip(
shadowColor: Colorsx.mainColor2,
elevation: 24,
label: LabelText1(document1["productName"] ?? ""),
backgroundColor: Colorsx.mainColor2,
),
],
),
),
],
),
);
},
),
StreamBuilder(
流:_databaseService.productCollection.doc(docID.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData){
返回中心(
子对象:循环压缩机指示器(
valueColor:
新的AlwaysStoppedAnimation(颜色X.mainColor),
),
);
}
var document1=snapshot.data;
返回容器(
装饰:盒子装饰(
颜色:Colorsx.mainColor,
边界半径:半径,
),
//颜色:Colorsx.mainColor,
子:列(
儿童:[
扩大(
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
芯片(
标签:LabelText1(“Ürün Adı:”),
背景颜色:Colorsx.mainColor,
),
芯片(
阴影颜色:Colorsx.mainColor2,
标高:24,
标签:LabelText1(document1[“productName”]??“”),
背景色:Colorsx.mainColor2,
),
],
),
),
],
),
);
},
),
我找不到这里的问题是什么,但根据我的研究,它与地图相关。
有什么想法吗?这看起来像是
\u databaseService.productCollection.doc(docID)
在代码运行时的某个时刻可能不会指向现有文档。然后,如果您像那样对其调用document1[“productName”]
,它将引发您看到的错误
因此,您需要决定在这种情况发生时渲染什么(即使只是短暂的渲染)。例如,您可以使循环压缩器指示器保持在屏幕上,直到文档可用:
if (!snapshot.hasData || !snapshot.data.exists) {
成功了,谢谢,先生:)