multi provider not changing state

Issue

I am developing a chat app and all was fine when i was using only a stream provider which takes user id stream from firebase, but as i want real time changes when i add a chat, so i added multi provider, and gives it stream provider and change notfier provider, now both are not working, i have to hot restart the app for changes.

 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(),
          ),
        ));

}

Solution

You can Simply Use 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;   }

Answered By – Tasnuva Tavasum oshin

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *