My debug statement is displaying the database correctly, by after something is going wrong and in getting the error: Unhandled Exception: NoSuchMethodError: Class 'int' has no instance method '[]'. I am getting null data when I'm trying to display is in my application, this is because the list i am using to store the values has length 0.
This is my class:
class Values {
num humidity;
num temperature;
num moisture;
bool led;
Values({this.humidity,this.temperature,this.moisture,this.led});
}
class RealtimeDB extends StatefulWidget {
@override
_RealtimeDBState createState() => _RealtimeDBState();
}
class _RealtimeDBState extends State<RealtimeDB> {
final databaseReference = FirebaseDatabase.instance.reference().child("Values");
final AuthService _auth = AuthService();
List<Values> list = new List();
@override
void initState() {
super.initState();
databaseReference.once().then((DataSnapshot snap) {
print("Data: ${snap.value}"); //Debug statement
var data=snap.value;
list.clear();
data.forEach((key,value){
Values val=new Values(
humidity: value["Humidity"],
moisture: value["Moisture"],
temperature: value["Temperature"],
led: value["MotorControl"],
);
list.add(val);
});
setState((){});
});
}
This is my listview builder:
body: new Container(
child: list.length==0?Text("Data is null"): new ListView.builder(
itemCount: list.length,
itemBuilder: (_,index){
return UI(list[index].humidity, list[index].temperature, list[index].moisture, list[index].led);
}
),
)
);
}
}
This is my simple database enter image description here
This is the console: enter image description here
Please any help would be appreciated.