gpt4 book ai didi

flutter - 从 firebase flutter 中的已知文档获取字段值

转载 作者:行者123 更新时间:2023-12-05 05:59:33 25 4
gpt4 key购买 nike

我希望用户在我的应用程序中提供一个值并让其他用户看到它。但是我可以将数据上传到 firebase 但不能在另一端获取。我在 flutter 上启用了空安全。代码是:-

child: StreamBuilder(
stream:FirebaseFirestore.instance.collection('Collection name').doc('doc id').snapshots()
builder: (context, snapshot){
if(snapshot.hasData){
return Text(snapshot.data['Field name'].toString()); // here my editor shows error saying 'The method '[]' can't be unconditionally invoked because the receiver can be 'null'.' and asks for a null check and then shows error again
}
return Text('Nothing');
}
),

编辑:我只收到“type '_JsonDocumentSnapshot' is not a subtype of type 'Map ' in type cast”错误

如果我改变

if(snapshot.hasData){
return Text(snapshot.data['Field name'].toString());

if (snapshot.hasData) {
return Text(snapshot.data.toString());
}

我得到输出为“‘_JsonDocumentSnapshot’的实例”数据应该在哪里。我正在使用空检查飞镖

最佳答案

谢谢大家的支持,但是我无意中找到并回答了。我读到它的数据类型是 DocumentSnapshot,这对我有用

builder: (context, snapshot) {
if (snapshot.hasData) {
var numGuess = snapshot.data as DocumentSnapshot;
return Text(numGuess['Field name'].toString());
}
return Text('Nothing');
}
This works for null safety.

关于flutter - 从 firebase flutter 中的已知文档获取字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68104543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com