【问题标题】:type 'String' is not a subtype of type 'int'“String”类型不是“int”类型的子类型
【发布时间】:2020-12-14 20:28:12
【问题描述】:

大家好,我是 Flutter 的新手,我正在尝试将 api 数据提取到 listview 中,得到以下错误类型 'String' is not a subtype of type 'int' of 'index' , type 'String' is不是“int”类型的子类型,我没有从这些代码中获取值。这里的问题是这里没有显示数据。

class MyClient extends MyClientService {
  final int id, clientId, acceptedBy, pincode;
  final double latitude, longitude;
  final String perpose,
      details,
      when,
      name,
      mobile,
      email,
      orgName,
      address,
      city,
      state,
      country,
      clientIp,
      device,
      createdAt,
      updatedAt;

  MyClient({
    ...
  });

  factory MyClient.fromJson(Map<String, dynamic> json) {
    return MyClient(
      ...
    );
  }

  Future<List<MyClient>> fetchClients() async {
    var datas = await super.fetchDatas();
    List dataList = json.decode(datas);
    print(dataList); // Data Getting here
    List<MyClient> clients =
        dataList.map((req) => MyClient.fromJson(req)).toList();
    print(clients); // Here nothing
    return clients;
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies flutter-test


    【解决方案1】:

    问题在于变量类型不匹配,因此您可以继续进行的一种方法是在工厂方法中将所需类型的字符串数据解析为所有在模态类中声明为非字符串的变量

    factory MyClient.fromJson(Map<String, dynamic> json) {
    return MyClient(
      id: int.parse(json['id']),
      clientId: int.parse(json['client_id']),
       longitude: double.parse(json['longitude']),
     );
    

    }

    【讨论】:

      【解决方案2】:

      这里缺少对象类型。这意味着一些数据来自 API 作为字符串。但这就是声明为 int。显示问题是数据不匹配。

      import 'dart:convert';
      import 'package:PandeetApp/services/myclient.dart';
      
      class MyClient extends MyClientService {
        final int id, clientId, acceptedBy, pincode;
        final double latitude, longitude;
        final String perpose,
            details,
            when,
            name,
            mobile,
            email,
            orgName,
            address,
            city,
            state,
            country,
            clientIp,
            device,
            createdAt,
            updatedAt;
      
        MyClient({
          this.id,
          this.clientId,
          this.perpose,
          this.details,
          this.when,
          this.acceptedBy,
          this.name,
          this.mobile,
          this.email,
          this.orgName,
          this.address,
          this.city,
          this.state,
          this.country,
          this.latitude,
          this.longitude,
          this.pincode,
          this.clientIp,
          this.device,
          this.createdAt,
          this.updatedAt,
        });
      
        factory MyClient.fromJson(Map<String, dynamic> json) {
          return MyClient(
            id: json['id'],
            clientId: json['client_id'],
            perpose: json['perpose'],
            details: json['details'],
            when: json['when'],
            acceptedBy: json['accepted_by'],
            name: json['name'],
            mobile: json['mobile'],
            email: json['email'],
            orgName: json['org_name'],
            address: json['address'],
            city: json['city'],
            state: json['state'],
            pincode: json['pincode'],
            country: json['country'],
            latitude: json['latitude'],
            longitude: json['longitude'],
            clientIp: json['client_ip'],
            device: json['device'],
            createdAt: json['created_at'],
            updatedAt: json['updated_at'],
          );
        }
      
        Future<List<MyClient>> fetchClients() async {
          var datas = await super.fetchDatas();
          List dataList = json.decode(datas);
          List<MyClient> clients =
              dataList.map((req) => MyClient.fromJson(req)).toList();
          return clients;
        }
      }
      

      代替

      import 'dart:convert';
      import 'package:PandeetApp/services/myclient.dart';
      
      class MyClient extends MyClientService {
        final int id, clientId, acceptedBy;
        final double latitude, longitude;
        final String perpose,
            details,
            when,
            name,
            mobile,
            email,
            orgName,
            address,
            city,
            state,
            pincode,
            country,
            clientIp,
            device,
            createdAt,
            updatedAt;
      
        MyClient({
          this.id,
          this.clientId,
          this.perpose,
          this.details,
          this.when,
          this.acceptedBy,
          this.name,
          this.mobile,
          this.email,
          this.orgName,
          this.address,
          this.city,
          this.state,
          this.country,
          this.latitude,
          this.longitude,
          this.pincode,
          this.clientIp,
          this.device,
          this.createdAt,
          this.updatedAt,
        });
      
        factory MyClient.fromJson(Map<String, dynamic> json) {
          return MyClient(
            id: json['id'],
            clientId: json['client_id'],
            perpose: json['perpose'],
            details: json['details'],
            when: json['when'],
            acceptedBy: json['accepted_by'],
            name: json['name'],
            mobile: json['mobile'],
            email: json['email'],
            orgName: json['org_name'],
            address: json['address'],
            city: json['city'],
            state: json['state'],
            pincode: json['pincode'],
            country: json['country'],
            latitude: json['latitude'],
            longitude: json['longitude'],
            clientIp: json['client_ip'],
            device: json['device'],
            createdAt: json['created_at'],
            updatedAt: json['updated_at'],
          );
        }
      
        Future<List<MyClient>> fetchClients() async {
          var datas = await super.fetchDatas();
          List dataList = json.decode(datas);
          List<MyClient> clients =
              dataList.map((req) => MyClient.fromJson(req)).toList();
          return clients;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-24
        • 2021-03-29
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        • 1970-01-01
        • 2020-04-22
        相关资源
        最近更新 更多