【问题标题】:(Resolved)(type 'String' is not a subtype of type 'int') - Flutter(已解决)(类型 'String' 不是类型 'int' 的子类型)- Flutter
【发布时间】:2020-06-01 00:49:43
【问题描述】:

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

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

_TypeError(类型'String'不是类型'int'的子类型)

显然,该应用程序在此更新之前运行良好,即使在从“int”更改为“String”之后,我也会遇到同样的错误,但反过来:

_TypeError(类型'int'不是类型'String'的子类型)

尽管我更改了值,但我仍然会出现相同的错误,很明显我正在使用的 RestApi 没有任何更改。

当我到达“Chip”时出现错误,在我将其更改为 String 后,我在“Number”中得到相同的错误,并且在我更改两个相同的错误后出现但相反的方式,正如我上面指出的那样

这里是 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

【问题讨论】:

  • 你能发布你的代码吗?
  • @MSARKrish 我已经添加了它
  • 你能发布 JSON 吗?
  • 发布您的 JSON 示例以及代码的哪一行导致错误?
  • 如果行号在堆栈中,则您的完整错误。或给出完整的堆栈跟踪

标签: json firebase flutter firebase-realtime-database dart


【解决方案1】:

如果没有明确指定变量的类型,则变量的类型是动态的。 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,
  });

【讨论】:

  • 我遇到了同样的问题,它帮助了我。
【解决方案2】:

我喜欢这个问题,在这种情况下,我确实将类型从 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);
      
}

【讨论】:

    【解决方案3】:

    只需将int chip 转换为String chip,将int numero 转换为String 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,
          });
    

    【讨论】:

    • 我知道,正如我在问题中解释的那样,我已经这样做了,它给了我同样的错误,但另一方面,错误变为:_TypeError (type 'int' is not 'String' 类型的子类型)
    【解决方案4】:

    在 Json 中,您将 Chip 和 Numero 作为字符串接收,但在您的模型文件中,您将其声明为整数。在模型文件中将数据类型更改为字符串。

    String numero;
    String chip;
    

    【讨论】:

    • 我知道,正如我在问题中解释的那样,我已经这样做了,它给了我同样的错误,但另一方面,错误变为:_TypeError (type 'int' is not 'String' 类型的子类型)
    • 我让它在 dartpad 中运行,并将这些类型更改为字符串:dartpad.dev/0e4cb75388b7c9e75b4d681901bd0de8
    【解决方案5】:

    根据您提供的 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,
        };
    }
    
    

    【讨论】:

      猜你喜欢
      • 2019-10-15
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2020-10-02
      • 2023-01-12
      • 2020-03-19
      • 2021-05-10
      • 2022-11-25
      相关资源
      最近更新 更多