Search code examples
flutterdartfirebase-realtime-database

"Snapping the values of keys in real-time database in firebase" with Dart


I am making a Flutter application that communicates with the ESP8266 through Firebase. So I am trying to get the variables of different sensors from Firebase into my Dart file.

enter image description here

But it keeps giving this error:

The getter 'value' isn't defined for the type 'DatabaseEvent'. Try importing the library that defines 'value', correcting the name to the name of an existing getter, or defining a getter or field named 'value'.

Here is the relevant code:

bool ledOn = false;    
bool heaterOn = false;    
double drunkAmount = 0.0;    
String temperature = '';

final database = FirebaseDatabase.instance.ref();
_DashboardContentState() {    
  database.child('ESP').once().then((snapshot) {    
    temperature = snapshot.value['temperature'];    
    drunkAmount = snapshot.value['drunkAmount'];    
    heaterOn = snapshot.value[''] == 0;    
    ledOn = snapshot.value['LED'] == 0;    
  });

  database.child('ESP').onChildChanged.listen((event) {    
    DataSnapshot snap = event.snapshot;   
    if (snap.key == 'drunkAmount') {    
      drunkAmount = snap.snapshot.value;    
      setState(() {});    
    }    
  });    
}

so how can I get these values or can someone explain the error to me?


Solution

  • I finally managed to solve it or wrap around it somehow. I trust Dhafin's answer should have worked, but it didn't, and I don't know why. So here is what I have done; now everything works normally.

    databaseReference.child('ESP').onValue.listen((event) {
      final DataSnapshot snapshot = event.snapshot;
      List values = snapshot.children.toList();
      setState(() {
        heaterOn = values[0].value;
        ledOn = values[1].value;
        drunkAmount = values[2].value;
        temperature = values[3].value;
      });
    });