These are the values read by sensor
Im trying to fetch these values from this database and use them in my application developed using flutter. But idk how. Any help is appreciated. language used is dart. I need to read four different values to four different variables
I tried many ways but ends up in error. i got one code thats not showing error but dont seem to read value
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
final databaseReference = FirebaseDatabase.instance.ref().child('Sensor');
class Fetch {
void getData() {
DatabaseReference ref = FirebaseDatabase.instance.ref('Sensor/');
ref.onValue.listen((DatabaseEvent event) {
var temp = event.snapshot.value;
print(temp);
});
}
}
3
Answers
without listen:
with listen:
You can get Stream from
FirebaseDatabase.instance.ref('Sensor/').onValue;
and useStreamBuilder
to display data. and you can separate the logic from UI code.sensors
instead ofSensor
. Lowercase and plural. See more about structure data in Firebase Realtime Database: https://firebase.google.com/docs/database/flutter/structure-datadata as Map
.