我正在開發一個聊天應用程式,當我只使用一個從 firebase 獲取用戶 ID 流的流提供程式時,一切都很好,但是因為我希望在添加聊天時實時更改,所以我添加了多個提供程式,并為其提供了流提供程式并更改 notfier 提供程式,現在兩者都不起作用,我必須熱重啟應用程式以進行更改。
Widget build(BuildContext context) {
return MultiProvider(
providers: [
// authentication provider
StreamProvider<User?>(
create: (context) => AuthController().userStream(),
initialData: null,
),
//states provider
ChangeNotifierProvider(create: (context) => Cloud()),
],
builder: (context, _) => MaterialApp(
theme: ThemeData(
primaryColor: Colors.deepPurpleAccent,
textTheme: TextTheme(button: TextStyle(color: Colors.white)),
primarySwatch: Colors.deepPurple),
home: Scaffold(
body: Wrapper(),
),
));
}
uj5u.com熱心網友回復:
您可以簡單地使用SteamBuilder& Firestore :
@override Widget build(BuildContext context) {
var streamBuilder = StreamBuilder<List<Message>>(
stream: getData(),
builder: (BuildContext context, AsyncSnapshot<List<Message>> messagesSnapshot) {
if (messagesSnapshot.hasError)
return new Text('Error: ${messagesSnapshot.error}');
switch (messagesSnapshot.connectionState) {
case ConnectionState.waiting: return new Text("Loading...");
default:
return new ListView(
children: messagesSnapshot.data.map((Message msg) {
return new ListTile(
title: new Text(msg.message),
subtitle: new Text(DateTime.fromMillisecondsSinceEpoch(msg.timestamp).toString()
"\n" (msg.user ?? msg.uid)),
);
}).toList()
);
}
}
);
return streamBuilder; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398589.html
