提问者:小点点

type'String'不是'int'类型的子类型


嗨,我是Flutter的新手,我正在尝试将api数据获取到listview中,得到了以下错误类型“String”不是“index”的“int”类型的子类型,“String”类型不是“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;
  }
}

共2个答案

匿名用户

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

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']),
 );

}

匿名用户

这里缺少对象类型。这意味着一些数据来自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;
  }
}