'Streamprovider does not listen to change of other provider flutter
I have multiple providers in my flutter app, through the provider package.
In main, before materialapp i have this:
ChangeNotifierProvider.value(
value: AppData(),
),
StreamProvider<List<ChatMessage>?>(
create: (context) => MessagesService().stream,
initialData: null,
),
The streamprovider relies on listening to the data of the appdata provider. It is supposed to listen to when user logs in or out.
class MessagesService {
StreamController<List<ChatMessage>> controller =
StreamController<List<ChatMessage>>.broadcast();
Stream<List<ChatMessage>?> get stream => controller.stream;
MessagesService() {
AppData data =
Provider.of<AppData>(AppData.navState.currentContext!, listen: false);
if (data.user != null) {
FirebaseFirestore.instance
.collection('messages')
.snapshots()
.listen((value) async {
controller.add(value.docs
.where((element) =>
element.data()['sender'] == data.user!.phone ||
element.data()['reciever'] == data.user!.phone)
.map((e) {
Map<String, dynamic> data = e.data() as Map<String, dynamic>;
data.addAll({'docId': e.id});
return ChatMessage.fromJson(data);
}).toList());
});
}
}
}
As you see there, i'm trying to listen to the user property of my provider, called AppData. It's listening when a user logs in, but if i log out, and then log in as another user, it seems to return the same value (so the data from the wrong user). Am I doing it correctly here where i am listening to the user with provider in appdata.
I know it is working in all other places, but in this provider not always.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
