(Resolved)(type 'String' is not a subtype of type 'int') - Flutter

5,474

Solution 1

If the type of a variable is not explicitly specified, the variable’s type is dynamic. The dynamic keyword can also be used as a type annotation explicitly.

Instead of int you can use dynamic and it will solve the issue.

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

Solution 2

I had like this issue and in this case I did defining type from int to dynamic then it solved. For example: In firebase side I defined number type and I read it with type of dynamic. If you do int in your codes it will warned you "type 'int' is not a subtype of type 'String'" but if you define dynamic it will solve. Code sample is in below.

//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);
      
}
Share:
5,474
Admin
Author by

Admin

Updated on December 17, 2022

Comments

  • Admin
    Admin over 1 year

    This question has already been answered, continue reading if you think you have the same error, the answer was given by the user: Tariqul Islam

    since a few days ago there was a flutter update my code shows the following error:

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

    Obviously the application worked perfectly before this update, even after changing from "int" to "String" the same error I get but the other way around:

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

    As much as I change the values ​​the same error still appears to me, it is also clear that the RestApi that I am using did not have any changes.

    I get the error when I get to "Chip", after I change it to String I get the same error in "Number", and after I change both the same error appears but the other way around as I indicated above

    Here the Json file model:

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

    This is the Json file Example:

                  {
                  "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"
                    }
                  ]
                }
    

    Could someone please help me? : c