我正在Flutter上构建一个应用程序,其中包括一个Firebase用户聊天功能。为了真实的显示数据更新,我通过StreamBuilder获取数据。一切正常,但当我决定关闭iPhone X上的WIFI和互联网连接时,我收到以下错误,应用程序关闭(注意在模拟器上没有出现任何错误,也没有关闭):
构建流式构建器的代码:
chatList(){
return StreamBuilder<QuerySnapshot>(
stream: DatabaseService(uid: FirebaseAuth.instance.currentUser!.uid).getChatInfo(),
builder: (context, snapshot){
//make some checks
if(snapshot.hasData){
var docs = snapshot.data!.docs;
if(docs.isNotEmpty){
return ListView.separated(
itemCount: docs.length,
itemBuilder: (context, index) {
var doc = docs[index];
return ChatTile(
bondName: (doc['bondNames'][0] == userName)? doc['bondNames'][1] : doc['bondNames'][0],
chatId: doc['chatId'],
sender: checkSender(doc['sentById'].toString()),
lastMessage: doc['lastMessage'].toString(),
elapsedTime: calculateElapsedTime((doc['sentAt'] as Timestamp).toDate()),
read: doc['read'],
userId: userId ,
);
},
separatorBuilder: (context, index) {
return const Divider();
},
);
}else{
return noChatWidget();
}
}else{
return Center(
child: CircularProgressIndicator(
color: Theme.of(context).primaryColor,
)
);
}
},
);
}
物理设备上的错误:
+88312 ms] 10.2.0 - [FirebaseFirestore][I-FST000001] WatchStream
(102d1d448) Stream error: 'Unavailable: Network connectivity changed'
[ +1 ms] 10.2.0 - [FirebaseFirestore][I-FST000001] WriteStream
(102d79128) Stream error: 'Unavailable: Network connectivity changed'
[+16360 ms] 10.2.0 - [FirebaseFirestore][I-FST000001] WatchStream
(102d1d448) Stream error: 'Unavailable: Network connectivity changed'
[ +8 ms] 10.2.0 - [FirebaseFirestore][I-FST000001] WatchStream
(102d1d448) Stream error: 'Unavailable: DNS resolution failed for
firestore.googleapis.com: UNKNOWN: nodename nor servname provided, or not
known'
[+4007 ms] 10.2.0 - [FirebaseFirestore][I-FST000001] WatchStream
(102d1d448) Stream error: 'Unavailable: DNS resolution failed for
firestore.googleapis.com: UNKNOWN: nodename nor servname provided, or not
known'
[ +11 ms] 10.2.0 - [FirebaseFirestore][I-FST000001] WatchStream
(102d1d448) Stream error: 'Unavailable: DNS resolution failed for
firestore.googleapis.com: UNKNOWN: nodename nor servname provided, or not
known'
[+1306 ms] Service protocol connection closed.
[ ] Lost connection to device.
[ +9 ms] DevFS: Deleting filesystem on the device
(file:///private/var/mobile/Containers/Data/Application/4D1F7639-FEF1-4A33
-866E-734CC70BE773/tmp/binderpC3ghF/binder/)
[ +253 ms] Ignored error while cleaning up DevFS: TimeoutException after
0:00:00.250000: Future not completed
[ +22 ms] "flutter run" took 1.660.540ms.
[ +31 ms] ensureAnalyticsSent: 13ms
[ +1 ms] Running shutdown hooks
[ ] Shutdown hooks complete
[ +1 ms] exiting with code 0
我试过检查互联网连接,并在流构建器上没有连接时返回null,但我仍然得到错误崩溃的应用程序。我已经搜索了所有地方,我仍然找不到为什么我得到错误知道Firebase甚至应该给予我一个离线选项,它只发生在物理设备上。谢谢!
1条答案
按热度按时间e4eetjau1#
使用StreamBuilder的ConnectionState属性检查流的状态,并在流处于ConnectionState.none状态时显示适当的消息或小部件。