[Flutter ]Unhandled Exception: NoSuchMethodError: The method '[]' was called on null
8,864
Your method getResistencia100()
is not returning anything. So at validate()
your variable resistencia100
is transforming into a null after await the getResistencia100()
A solution is change the getResistencia100()
, adding a return
statement
Future<List<Resistencia100>> getResistencia100() async {
return await ClientDatabaseProvider.db.getAllResistencia100();
}
Author by
Eduardo Gonzalez
Updated on December 16, 2022Comments
-
Eduardo Gonzalez over 1 year
class Resistencia100{ int id; double r_pos1; double r_pos2; double r_pos3; double r_pos4; double r_pos5; Resistencia100({ this.id, this.r_pos1, this.r_pos2, this.r_pos3, this.r_pos4, this.r_pos5 }); Map<String, dynamic> toMap() => { "id": id, "r_pos1": r_pos1, "r_pos2": r_pos2, "r_pos3": r_pos3, "r_pos4": r_pos4, "r_pos5": r_pos5, }; factory Resistencia100.fromMap(Map<String, dynamic> json) => new Resistencia100( id: json["id"], r_pos1: json["r_pos1"], r_pos2: json["r_pos2"], r_pos3: json["r_pos3"], r_pos4: json["r_pos4"], r_pos5: json["r_pos5"], ); }
This is my Model class Resistencia100, Now we will see how I request the data through my get method
Future<List<Resistencia100>> getAllResistencia100() async { final db = await database; var response = await db.query("Resistencia100"); List<Resistencia100> list = response.map((c) => Resistencia100.fromMap(c)).toList(); print("Cantidad ID: "+list[0].id.toString()); print("Cantidad r_pos1: "+list[0].r_pos1.toString()); print("Cantidad r_pos2: "+list[0].r_pos2.toString()); print("Cantidad r_pos3: "+list[0].r_pos3.toString()); print("Cantidad r_pos4: "+list[0].r_pos4.toString()); print("Cantidad r_pos5: "+list[0].r_pos5.toString()); return list; }
The information is coming correctly to the method, now I try to extract that information and the error is coming.
List <Resistencia100> resistencia100 = new List<Resistencia100>(); Future<List<Resistencia100>> getResistencia100() async { await ClientDatabaseProvider.db.getAllResistencia100(); } void validate() async { resistencia100 = await getResistencia100(); print("RESISTENCIA ID: "+resistencia100[0].id.toString()); }
The truth is that I don't understand the reason for the error very well, I hope you can understand, I will leave the textual error in the following lines, this is generated in the "print".
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: [](0) #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) #1 _ConfigConcretoState.validate (package:entremuros/vistas/configconcreto.dart:282:44)