【问题标题】:[Flutter ]Unhandled Exception: NoSuchMethodError: The method '[]' was called on null[颤振]未处理的异常:NoSuchMethodError:方法'[]'在null上被调用
【发布时间】:2020-04-25 00:58:31
【问题描述】:
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"],
  );

}

这是我的模型类 Resistencia100,现在我们将看看我如何通过我的 get 方法请求数据

  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;
  }

信息正确地传递给方法,现在我尝试提取该信息并且错误来了。


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());


  }

事实是我不是很了解错误的原因,希望您能理解,我将在以下几行中留下文本错误,这是在“打印”中生成的。

[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)

【问题讨论】:

    标签: android json flutter


    【解决方案1】:

    您的方法getResistencia100() 没有返回任何内容。所以在validate() 你的变量resistencia100 在等待getResistencia100() 之后变成了一个空值

    解决方案是更改getResistencia100(),添加return 语句

    Future<List<Resistencia100>> getResistencia100() async {
        return await ClientDatabaseProvider.db.getAllResistencia100();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2022-11-17
      • 1970-01-01
      • 2021-06-08
      • 2020-09-28
      • 2019-07-25
      • 1970-01-01
      • 2020-12-09
      • 2021-09-26
      相关资源
      最近更新 更多