(已解决)(类型 'String' 不是类型"int"的子类型) - 颤振



这个问题已经得到回答,如果你认为自己有同样的错误,请继续阅读,答案由用户给出:Tariqul Islam

由于几天前有一个颤振更新,我的代码显示了以下错误:

_TypeError(类型"String"不是类型"int"的子类型(

显然,在这次更新之前,应用程序运行得很好,即使在从"int"更改为"String"之后,我也会遇到同样的错误,但情况正好相反:

_TypeError(类型"int"不是类型"String"的子类型(

只要我更改值​​同样的错误仍然出现在我的脑海中,很明显,我使用的RestApi没有任何变化。

当我到达"芯片"时,我得到了错误,在我将其更改为字符串后,我在"数字"中得到了相同的错误,并且在我更改两者后,出现了相同的误差,但与我在上指出的相反

这里的Json文件模型:

class EventoModel {
String id;
String nombreEvento;
List<Participantes> participantes;
EventoModel({
this.id,
this.nombreEvento,
this.participantes
});
factory EventoModel.fromJson(Map<String, dynamic> parsedJson){
var list = parsedJson['participantes'] as List;
//print(list.runtimeType);
List<Participantes> participantesList = list.map((i) => Participantes.fromJson(i)).toList();
return EventoModel(
id            : parsedJson ['id'],
nombreEvento  : parsedJson ['nombreEvento'],
participantes : participantesList
);
}
}
class Participantes {
String uniqueId;
String apellido;
int chip;
String nombre;
int numero;
String place;
String tiempo;
Participantes({
this.apellido,
this.chip,
this.nombre,
this.numero,
this.place,
this.tiempo,
});
factory Participantes.fromJson(Map<String, dynamic> parsedJson) {
//print(list.runtimeType);
return Participantes(
apellido  : parsedJson['Apellido'],
chip      : parsedJson['Chip'],
nombre    : parsedJson['Nombre'],
numero    : parsedJson['Numero'],
place     : parsedJson['Place'],
tiempo    : parsedJson['Tiempo'],
);
}
Map<String, dynamic> toJson() {
return {
'Apellido'  : apellido,
'Chip'      : chip,
'Nombre'    : nombre,
'Numero'    : numero,
'Place'     : place,
'Tiempo'    : tiempo,
};
}
}

这是Json文件示例:

{
"nombreEvento" : "Clasico El Colombiano 2020",
"participantes" : [ {
"Apellido" : "MARTINEZ GUTIERREZ",
"Chip" : "739",
"Nombre" : "JOSE",
"Numero" : "139",
"Place" : "1.",
"Tiempo" : "00:30:12,91"
}, {
"Apellido" : "SUAREZ MORERA",
"Chip" : "707",
"Nombre" : "DANIEL",
"Numero" : "107",
"Place" : "2.",
"Tiempo" : "02:00:17,54"
}, {
"Apellido" : "RODRIGUEZ VARGAS",
"Chip" : "1686",
"Nombre" : "JOSE LUIS",
"Numero" : "274",
"Place" : "3.",
"Tiempo" : "02:01:09,09"
}
]
}

有人能帮我吗c

如果没有显式指定变量的类型,则变量的类型是动态的。dynamic关键字也可以显式地用作类型注释。

您可以使用dynamic而不是int,它将解决问题。

class Participantes {
String uniqueId;
String apellido;
dynamic chip;
String nombre;
dynamic numero;
String place;
String tiempo;
Participantes({
this.apellido,
this.chip,
this.nombre,
this.numero,
this.place,
this.tiempo,
});

我喜欢这个问题,在这种情况下,我定义了从int到dynamic的类型,然后它就解决了。例如:在firebase方面,我定义了数字类型,并用动态类型来阅读它。如果你在代码中执行int,它会警告你";类型"int"不是类型"String"的子类型"但如果你定义了动态,它就会解决问题。下面是代码示例。

//class Survey
class Survey {
String name;
dynamic vote;  // before it was int type and I have changed
DocumentReference reference;

Survey.fromMap(Map<String, dynamic> map, {this.reference})
//datanın var olup olmadığını kontrol et eğer varsa kullan
: assert(map["name"] != null),
assert(map["vote"] != null),
name = map["name"],
vote = map["vote"];

Anket.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);

}

只需执行int chipString chip,以及int numeroString numero,因为在JSON中,数据来自String

class Participantes {
String uniqueId;
String apellido;
String chip;
String nombre;
String numero;
String place;
String tiempo;
Participantes({
this.apellido,
this.chip,
this.nombre,
this.numero,
this.place,
this.tiempo,
});

在Json中,您将Chip和Numero作为String接收,但在模型文件中,您声明为integer。在模型文件中将数据类型更改为String。

String numero;
String chip;

根据您提供的JSON,我制作了下面的模型类:查看并告诉我:

// To parse this JSON data, do
//
//     final eventoModel = eventoModelFromJson(jsonString);
import 'dart:convert';
EventoModel eventoModelFromJson(String str) => EventoModel.fromJson(json.decode(str));
String eventoModelToJson(EventoModel data) => json.encode(data.toJson());
class EventoModel {
String nombreEvento;
List<Participante> participantes;
EventoModel({
this.nombreEvento,
this.participantes,
});
factory EventoModel.fromJson(Map<String, dynamic> json) => EventoModel(
nombreEvento: json["nombreEvento"],
participantes: List<Participante>.from(json["participantes"].map((x) => Participante.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"nombreEvento": nombreEvento,
"participantes": List<dynamic>.from(participantes.map((x) => x.toJson())),
};
}
class Participante {
String apellido;
String chip;
String nombre;
String numero;
String place;
String tiempo;
Participante({
this.apellido,
this.chip,
this.nombre,
this.numero,
this.place,
this.tiempo,
});
factory Participante.fromJson(Map<String, dynamic> json) => Participante(
apellido: json["Apellido"],
chip: json["Chip"],
nombre: json["Nombre"],
numero: json["Numero"],
place: json["Place"],
tiempo: json["Tiempo"],
);
Map<String, dynamic> toJson() => {
"Apellido": apellido,
"Chip": chip,
"Nombre": nombre,
"Numero": numero,
"Place": place,
"Tiempo": tiempo,
};
}

相关内容

最新更新