'Realtime database Provider Flutter stream events
I am trying to stream events from a particular child node in Realtime database using Provider in flutter. I have created a Provider Class as below
class RTDBService {
FirebaseDatabase database = FirebaseDatabase.instance;
StreamSubscription<DatabaseEvent> getCurrencies() {
return database.ref('Currencies').onValue.listen((event){
final data = Map<String, dynamic>.from(event.snapshot.value);
final currencyList = Currency.fromJson(data);
return currencyList;
});
}
}
The Currency class is as below import 'package:cloud_firestore/cloud_firestore.dart';
class Currency {
final List? name;
final Timestamp? lastDatabaseTimeUpdated;
Currency({
this.name,
this.lastDatabaseTimeUpdated,
});
factory Currency.fromJson(Map<String, dynamic> json) {
return Currency(
name: json['rates'] as List,
lastDatabaseTimeUpdated: json['lastDatabaseTimeUpdated'] as Timestamp);
}
}
and the provider is wrapped as below
MultiProvider(
providers: [
Provider<RTDBService>(
create: (_) => RTDBService(),
),
StreamProvider(
initialData: null,
create: (context) => context.read<RTDBService>().getCurrencies(),
),
],
There are so many tutorials explaining how to use Provider with Firestore but close to none pointing how to use Provider with Realtime Database How can I go about streaming these events using Provider. A link to an article detailing the use of Provider with Realtime Database with flutter would also be welcome
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
