skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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;
      });
    });
    

  2. The returned future value of the once method is a DatabaseEvent, not a DataSnapshot. So your snapshot variable is actually a DatabaseEvent. To get the snapshot of a DatabaseEvent, call its snapshot getter.

    database.child('ESP').once().then((event) {
      final snapshot = event.snapshot;
      
      temperature = snapshot.value['temperature'];
      // ...
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search