多供应商不改变状态
multi provider not changing state
我正在开发一个聊天应用程序,当我只使用一个从 firebase 获取用户 ID 流的流提供者时一切都很好,但是因为我想要在添加聊天时进行实时更改,所以我添加了多个提供者,并为其提供流提供程序和更改通知提供程序,现在两者都不起作用,我必须热重启应用程序才能进行更改。
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(),
),
));
}
您可以简单地使用 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; }
我正在开发一个聊天应用程序,当我只使用一个从 firebase 获取用户 ID 流的流提供者时一切都很好,但是因为我想要在添加聊天时进行实时更改,所以我添加了多个提供者,并为其提供流提供程序和更改通知提供程序,现在两者都不起作用,我必须热重启应用程序才能进行更改。
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(),
),
));
}
您可以简单地使用 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; }