gpt4 book ai didi

android - [ flutter ]未处理的异常 : NoSuchMethodError: The method '[]' was called on null

转载 作者:行者123 更新时间:2023-12-04 10:47:38 25 4
gpt4 key购买 nike

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)

最佳答案

您的方法 getResistencia100()没有返回任何东西。所以在 validate()您的变量 resistencia100在等待 getResistencia100() 后转换为空值

解决方案是更改 getResistencia100() ,添加一个 return陈述

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

关于android - [ flutter ]未处理的异常 : NoSuchMethodError: The method '[]' was called on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59629785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com