json - (已解决)(类型 'String' 不是类型 'int' 的子类型)- Flutter

标签 json firebase flutter firebase-realtime-database dart

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

自从几天前有一次 flutter 更新后,我的代码显示了以下错误:

_TypeError (type 'String' is not a subtype of type 'int')

很明显,应用程序在此更新之前运行良好,即使在从“int”更改为“String”之后我得到了相同的错误,但相反:

_TypeError (type 'int' is not a subtype of type '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

最佳答案

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

关于json - (已解决)(类型 'String' 不是类型 'int' 的子类型)- Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60256505/

相关文章:

java - 适用于 Android 的 firebase 云存储和 firestore 上传

Flutter:是否可以在高度> 1.0 的文本行中设置垂直对齐方式?

android - 我可以在不下载 android studio 的情况下使用 flutter 吗?

json - 如何将 header 添加到 JSON 以识别数组值的数组名称

php - 格式错误的 JSON 字符串需要针对 PHP 进行更正

ios - Firebase 推送通知 iOS 问题,无法获取 APNS token 错误域=com.firebase.iid 代码=1001 "(null)"

http - DioError [DioErrorType.RESPONSE] : Http status error [405] [Solved]

python - GitLab API - 如何获取存储库/项目文件和元数据?

ios - 在 Swift 中将 socket.io 数据转换为 JSON

firebase - *运行Gradle时出错:ProcessException:进程 “E:\Flutter\login_demo\android\gradlew.bat”异常退出: