0

currently i'm using firestore and realtime database at the same time. I set and retrieve from firestore in the most simplest and effective way in code and for realtime database i set data but i couldn't retrieve it in the same way that i do with firestore. Summary i want to do the same thing which i do with firestore code in realtime database code.

Here is my code:

//Get data from Firestore 

Stream <DocumentSnapshot> getData() async*{
    final user = FirebaseAuth.instance.currentUser; 
    yield* FirebaseFirestore.instance.collection('users').doc(user.uid).snapshots();
    }

//Return data in StreamBuilder (No lists or ListView.Builder needed here)

@override
  Widget build(BuildContext context) {

    return StreamBuilder(
      stream: getData(),
      builder: (context, snapshot) {

        //--------------------------------------
        //These equations comes from Firestore
        //--------------------------------------

        int currentWater ()=>  snapshot.data['currentLitersAmount'];

        int remainingWater () => snapshot.data['currentLitersAmount'] <= snapshot.data['recomendedLitersAmount'] ? snapshot.data['recomendedLitersAmount'] - snapshot.data['currentLitersAmount'] : 0;
        
        double progress ()=> snapshot.data['currentLitersAmount'] / snapshot.data['recomendedLitersAmount'];

So how to do the same thing here for realtime database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Abddi
  • 25
  • 6

1 Answers1

0

The equivalent of your getData function for Realtime Database would be:

Stream <Event> getData() async*{
    final user = FirebaseAuth.instance.currentUser; 
    yield* FirebaseDatabase.instance.reference().child('users').child(user.uid).onValue();
}

And you can then get the DataSnapshot from each Event object in your UI building code.

@override
Widget build(BuildContext context) {

  return StreamBuilder(
    stream: getData(),
    builder: (context, snapshot) {
      int currentWater ()=>  snapshot.data.snapshot.value['currentLitersAmount'];
      ...

If that snapshot.snapshot looks confusing, have a look at What is the difference between existing types of snapshots in Firebase?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Actually it gave me an error in code to be exact in the second snapshot: here => snapshot.snashot.value, So i have tried snapshot.data.value but also it returned an error (Class 'Event' has no instance getter 'value'. Receiver: Instance of 'Event' Tried calling: value) – Abddi Nov 20 '21 at 04:49
  • `onValue` returns an `Event`: https://pub.dev/documentation/firebase_database/latest/firebase_database/Query/onValue.html, so the `snapshot` is an [event object](https://pub.dev/documentation/firebase_database/latest/firebase_database/Event-class.html), from which you should be able to get the value. That's why you need `snapshot.snapshot`. – Frank van Puffelen Nov 20 '21 at 05:10
  • You're 100% correct but here the issue is when i write the code it gave me an error line under: 1. Stream.... reference('users') <= red-line under 'users'. 2. StreamBuilder(.... snapshot.(snapshot) <= red-line under it – Abddi Nov 20 '21 at 05:26
  • unfortunately i can't find a way to share screen shots here to show you exactly what is happening – Abddi Nov 20 '21 at 05:32
  • If you want to share a screenshot of the error, edit your question to include that screenshot. But I think I see what's wrong there now, so updated the code in my answer. I highly recommend keeping the reference documentation handy, as it'll be a lot faster if you can look up such syntax problems yourself. https://pub.dev/documentation/firebase_database/latest/firebase_database/FirebaseDatabase/reference.html – Frank van Puffelen Nov 20 '21 at 15:55
  • It worked, firebase has changed the code to snapshot.data.snapshot.value[''], Thank you so much – Abddi Nov 22 '21 at 07:39