Home > Net >  The getter 'value' isn't defined for the type 'DatabaseEvent'
The getter 'value' isn't defined for the type 'DatabaseEvent'

Time:10-21

i have this problem at line snapshot.value

  @override
  void initState() {
    super.initState();
    databaseReference
        .child('ESP32_Device')
        .once()
        .then((DatabaseEvent snapshot) {
      double temp = snapshot.value['Temperature']['Data'];
      double humidity = snapshot.value['Humidity']['Data'];
      isLoading = true;
      _DashboardInit(temp, humidity);
    });
  }

photo of problem

CodePudding user response:

Seems null issue, try like this

double temp =
    double.tryParse("${snapshot?.value['Temperature']['Data']}") ?? 0.0;
double humidity =
    double.tryParse("${snapshot?.value['Humidity']['Data']}") ?? 0.0;

CodePudding user response:

Just because you create a variable named snapshot doesn't mean it is one, it is still a DatabaseEvent, not a snapshot.

I suggest you call the variable event, then use event.snapshot.value to access what you need.

Going forward I would recommend that you name variables for what they actually are eg somethingList (you can use plural instead here), somethingEvent, etc. It will save you, and other readers of your code, a lot of confusion. Point is to name stuff so that you know what it is.

  • Related