Firebase 颤振:在null上调用的getter文档显示了一段时间
我正在从无状态小部件调用一个方法。此文档从firebase数据库进行查询。但当它在输出中显示一个错误一段时间后,它就会显示结果。我怎样才能停止那3秒钟的错误呢。这是显示的错误 对null调用了getter“documents” 这是我的密码Firebase 颤振:在null上调用的getter文档显示了一段时间,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在从无状态小部件调用一个方法。此文档从firebase数据库进行查询。但当它在输出中显示一个错误一段时间后,它就会显示结果。我怎样才能停止那3秒钟的错误呢。这是显示的错误 对null调用了getter“documents” 这是我的密码 class chats extends StatelessWidget { @override Widget build(BuildContext context) { child: Container(child:getlast()) }} 这就是
class chats extends StatelessWidget {
@override
Widget build(BuildContext context) {
child: Container(child:getlast())
}}
这就是肉食法
getlast(){
var groupChatId = 123456789;
var id = chat.UID;
return FutureBuilder (
future: Firestore.instance
.collection("messages")
.document(groupChatId)
.collection(groupChatId)
.orderBy("timestamp", descending: true)
.limit(1)
.getDocuments(),
builder: (context,AsyncSnapshot snapshot) {
var con = snapshot.data.documents[0].data["content"];
Container(
height: 27,
child: Text("You: $con",
overflow: TextOverflow.fade,
style: TextStyle(
fontSize: 20),
)});
}
使用
FutureBuilder
时,需要检查连接状态
:
FutureBuilder(
future: Firestore.instance
.collection("messages")
.document(groupChatId)
.collection(groupChatId)
.orderBy("timestamp", descending: true)
.limit(1)
.getDocuments(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
var con = snapshot.data.documents[0].data["content"];
Container(
height: 27,
child: Text("You: $con",
overflow: TextOverflow.fade,
style: TextStyle(
fontSize: 20),
)});
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
return CircularProgressIndicator();
},
),
FutureBuilder(
未来:Firestore.instance
.收集(“信息”)
.document(groupChatId)
.collection(groupChatId)
.orderBy(“时间戳”,降序:true)
.限额(1)
.getDocuments(),
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
var con=snapshot.data.documents[0]。数据[“content”];
容器(
身高:27,
孩子:文本(“你:$con”,
溢出:TextOverflow.fade,
样式:TextStyle(
尺寸:20),,
)});
}else if(snapshot.connectionState==connectionState.none){
返回文本(“无数据”);
}
返回循环ProgressIndicator();
},
),
documents
被调用为null,这意味着仍然没有检索到数据,因此通过使用connectionState.done
可以确保检索到数据
使用
FutureBuilder
时,需要检查连接状态
:
FutureBuilder(
future: Firestore.instance
.collection("messages")
.document(groupChatId)
.collection(groupChatId)
.orderBy("timestamp", descending: true)
.limit(1)
.getDocuments(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
var con = snapshot.data.documents[0].data["content"];
Container(
height: 27,
child: Text("You: $con",
overflow: TextOverflow.fade,
style: TextStyle(
fontSize: 20),
)});
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
return CircularProgressIndicator();
},
),
FutureBuilder(
未来:Firestore.instance
.收集(“信息”)
.document(groupChatId)
.collection(groupChatId)
.orderBy(“时间戳”,降序:true)
.限额(1)
.getDocuments(),
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
var con=snapshot.data.documents[0]。数据[“content”];
容器(
身高:27,
孩子:文本(“你:$con”,
溢出:TextOverflow.fade,
样式:TextStyle(
尺寸:20),,
)});
}else if(snapshot.connectionState==connectionState.none){
返回文本(“无数据”);
}
返回循环ProgressIndicator();
},
),
documents
被调用为null,这意味着仍然没有检索到数据,因此通过使用connectionState.done
可以确保检索到数据
在getLast函数名之前添加返回类型在getLast函数名之前添加返回类型感谢您的关注,但它只返回“无数据”。尽管数据库中存在数据,但您确定路径正确吗?你检查过ID是否为空吗?是的,我做了所有事情删除了这个
child:Container(child:getlast())
,而不是使用child:FutureBuilder(
)和我回答中的代码。连接状态总是在等待感谢你的关注,但它只返回“无数据”。尽管数据库中存在数据,但您是否确定路径正确?您是否检查了ID是否为空?是的,我做了所有操作,删除了此子容器(child:getlast())
,而不是使用子容器:FutureBuilder(
)和我回答中的代码。连接状态一直在等待