'Flutter Firebase RTDB onValue triggered multiple times
I'm developing an app which needs live data from the database which was created using Firebase RTDB. I used onValue to get the live data, the problem is that the onValue is triggered multiple times when a lot of data changed at once and messed up my data processing system. Been trying to look for solutions, but still haven't managed to find one. Any help will be appreciated. Here's the code:
await refConnection
.child(user!.uid).orderByChild('date_created').onValue.forEach((element) {
if (element.snapshot.value != null) {
Map<dynamic, dynamic> connection = element.snapshot.value as Map<
dynamic,
dynamic>;
print("Test: $connection");
connection.forEach((key, value) {
if (connection[key]['status'] == 'connected') {
Provider.of<ContactDataProvider>(context, listen: false)
.addConnected(connection[key]);
} else if (connection[key]['status'] == 'incoming_req') {
Provider.of<ContactDataProvider>(context, listen: false)
.addIncoming(connection[key]);
} else if (connection[key]['status'] == 'sent_req') {
print(connection[key]);
Provider.of<ContactDataProvider>(context, listen: false)
.addSent(connection[key]);
}
});
setState();
}
});
Solution 1:[1]
Do you really need to listen for live updates? If not, then try getting the value only once using get().
Solution: Trying with a different approach
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | treble18 |
