0
votes

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.

2
Can you add Value model also.. And specify the line where you are getting the error in above code? + Just suggestion, you can call UI(list[index]) instead of passing all the arguments - Pushpendra
What is your Motor Control type? Because in screenshot its seems it's holding a single quote - DinkarKumar
Motor control type is boolean @dinkar_kumar - advik Maniar
@Pushpendra I just added the value model above, and thankyou for the suggestion! - advik Maniar
@Pushpendra I think I am getting the error after my debug statement. - advik Maniar

2 Answers

0
votes

Your data looks like this -

{Temperature: 15, Moisture: 14, Humidity: 12, MotorControl: false }

data.forEach((key, value) => WHATEVER), here your value will be:

15, 14, 12, false..

These are not list and you are accessing them as value["WHATEVER"]. That's why. you are getting. "Class 'int' has no instance method '[]'"

What you can do -

list.add(
Values(
    humidity: int.parse(value["Humidity"]),
    moisture: int.parse(value["Moisture"]),
    temperature: int.parse(value["Temperature"]),
    led: value["MotorControl"],
),
);
0
votes

It seems that you are getting error while trying to fetch the values

  humidity: value["Humidity"],

It is because snaps.value is a Json or rather Map Object you can just create a Utility fromMap and add it to your Values class and also since its a Map you dont need to loop throught it.

Values.fromMap(Map<String, dynamic> map):
    humidity: int.parse(value["Humidity"].toString()),
    moisture: int.parse(value["Moisture"].toString()),
    temperature: int.parse(value["Temperature"].toString()),
    led: value["MotorControl"],

and then you could use it like this

 @override
  void initState() {
    super.initState();
    databaseReference.once().then((DataSnapshot snap) {
      print("Data: ${snap.value}");  //Debug statement
      Map data=snap.value;
      final value = Value.fromMap(data);
        list.add(value);
      setState((){});
  }
}