Json模型类没有getter列表接收者实例

问题描述 投票:0回答:1

简单的JSON数据获取并在屏幕上显示,数据正在打印中显示,但是在屏幕上显示NoSuchMethodErrorClass'ResponsiveObjectSettingByUser'的给定错误时,没有实例方法'car'接收者:Instance,

获取功能

Future<ResponseObjectSettingByUser> _list;
  final String _url = '<LINK>';


Future<ResponseObjectSettingByUser> getUserSettings() async {
    ResponseObjectSettingByUser responseData = null;

    var response = await http
        .get(_url + '<link>', headers: {
      "Authorization":
          'Bearer <TOKEN>',
    });

     var responseJson = json.decode(response.body);
    if (response.statusCode == 200) {
      responseJson = settingByUserModel(response.body);
      print(responseJson.toJson().toString());
    }
    return responseJson;
  }

初始化

 @override
  void initState() {
    super.initState();

       _list = getUserSettings();
  }

模型类(ResponseObjectSettingByUser)

// To parse this JSON data, do
//
//     final welcome = welcomeFromJson(jsonString);

import 'dart:convert';

ResponseObjectSettingByUser settingByUserModel(String str) => ResponseObjectSettingByUser.fromJson(json.decode(str));

String welcomeToJson(ResponseObjectSettingByUser data) => json.encode(data.toJson());

class ResponseObjectSettingByUser {
  ResponseObjectSettingByUser({
    this.array,
  });

  Array array;

  factory ResponseObjectSettingByUser.fromJson(Map<String, dynamic> json) => ResponseObjectSettingByUser(
    array: Array.fromJson(json["array"]),
  );

  Map<String, dynamic> toJson() => {
    "array": array.toJson(),
  };
}

class Array {
  Array({
    this.car,
  });

  List<Car> car;

  factory Array.fromJson(Map<String, dynamic> json) => Array(
    car: List<Car>.from(json["car"].map((x) => Car.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "car": List<dynamic>.from(car.map((x) => x.toJson())),
  };
}

class Car {
  Car({
    this.id,
    this.name,
    this.numberKmsMin,
    this.numberKmsMax,
    this.priceMin,
    this.priceMax,
    this.powerHorseMin,
    this.powerHorseMax,
    this.status,
    this.isSaved,
    this.markId,
    this.markName,
    this.markImageUrl,
    this.modelId,
    this.modelName,
    this.modelImageUrl,
    this.bodyworkId,
    this.bodyworkName,
    this.fuelId,
    this.fuelName,
    this.motorizationId,
    this.motorizationName,
    this.rimsId,
    this.rimsName,
    this.serieId,
    this.serieName,
    this.interiorEquipmentId,
    this.interiorEquipmentName,
    this.upholsteryId,
    this.upholsteryName,
    this.upholsteryLeatherFabricName,
    this.iluminationId,
    this.iluminationName,
    this.externalEquipmentId,
    this.externalEquipmentName,
    this.dateStartMin,
    this.dateEndMax,
    this.settings,
  });

  int id;
  String name;
  String numberKmsMin;
  String numberKmsMax;
  String priceMin;
  String priceMax;
  String powerHorseMin;
  String powerHorseMax;
  String status;
  int isSaved;
  int markId;
  String markName;
  String markImageUrl;
  int modelId;
  String modelName;
  String modelImageUrl;
  int bodyworkId;
  String bodyworkName;
  int fuelId;
  String fuelName;
  int motorizationId;
  String motorizationName;
  dynamic rimsId;
  dynamic rimsName;
  int serieId;
  String serieName;
  int interiorEquipmentId;
  String interiorEquipmentName;
  int upholsteryId;
  String upholsteryName;
  String upholsteryLeatherFabricName;
  int iluminationId;
  String iluminationName;
  int externalEquipmentId;
  String externalEquipmentName;
  String dateStartMin;
  String dateEndMax;
  List<Setting> settings;

  factory Car.fromJson(Map<String, dynamic> json) => Car(
    id: json["id"],
    name: json["name"],
    numberKmsMin: json["number_kms_min"],
    numberKmsMax: json["number_kms_max"],
    priceMin: json["price_min"],
    priceMax: json["price_max"],
    powerHorseMin: json["power_horse_min"],
    powerHorseMax: json["power_horse_max"],
    status: json["status"],
    isSaved: json["is_saved"],
    markId: json["mark_id"],
    markName: json["mark_name"],
    markImageUrl: json["mark_image_url"],
    modelId: json["model_id"],
    modelName: json["model_name"],
    modelImageUrl: json["model_image_url"],
    bodyworkId: json["bodywork_id"],
    bodyworkName: json["bodywork_name"],
    fuelId: json["fuel_id"],
    fuelName: json["fuel_name"],
    motorizationId: json["Motorization_id"],
    motorizationName: json["Motorization_name"],
    rimsId: json["rims_id"],
    rimsName: json["rims_name"],
    serieId: json["serie_id"],
    serieName: json["serie_name"],
    interiorEquipmentId: json["Interior_equipment_id"],
    interiorEquipmentName: json["Interior_equipment_name"],
    upholsteryId: json["Upholstery_id"],
    upholsteryName: json["Upholstery_name"],
    upholsteryLeatherFabricName: json["Upholstery_Leather_fabric_name"],
    iluminationId: json["ilumination_id"],
    iluminationName: json["ilumination_name"],
    externalEquipmentId: json["external_equipment_id"],
    externalEquipmentName: json["external_equipment_name"],
    dateStartMin: json["date_start_min"],
    dateEndMax: json["date_end_max"],
    settings: json["settings"] == null ? null : List<Setting>.from(json["settings"].map((x) => Setting.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "name": name,
    "number_kms_min": numberKmsMin,
    "number_kms_max": numberKmsMax,
    "price_min": priceMin,
    "price_max": priceMax,
    "power_horse_min": powerHorseMin,
    "power_horse_max": powerHorseMax,
    "status": status,
    "is_saved": isSaved,
    "mark_id": markId,
    "mark_name": markName,
    "mark_image_url": markImageUrl,
    "model_id": modelId,
    "model_name": modelName,
    "model_image_url": modelImageUrl,
    "bodywork_id": bodyworkId,
    "bodywork_name": bodyworkName,
    "fuel_id": fuelId,
    "fuel_name": fuelName,
    "Motorization_id": motorizationId,
    "Motorization_name": motorizationName,
    "rims_id": rimsId,
    "rims_name": rimsName,
    "serie_id": serieId,
    "serie_name": serieName,
    "Interior_equipment_id": interiorEquipmentId,
    "Interior_equipment_name": interiorEquipmentName,
    "Upholstery_id": upholsteryId,
    "Upholstery_name": upholsteryName,
    "Upholstery_Leather_fabric_name": upholsteryLeatherFabricName,
    "ilumination_id": iluminationId,
    "ilumination_name": iluminationName,
    "external_equipment_id": externalEquipmentId,
    "external_equipment_name": externalEquipmentName,
    "date_start_min": dateStartMin,
    "date_end_max": dateEndMax,
    "settings": settings == null ? null : List<dynamic>.from(settings.map((x) => x.toJson())),
  };
}

class Setting {
  Setting({
    this.id,
    this.name,
  });

  int id;
  String name;

  factory Setting.fromJson(Map<String, dynamic> json) => Setting(
    id: json["id"],
    name: json["name"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "name": name,
  };
}

使用]在屏幕上显示数据>

   child: FutureBuilder(
                              future: getUserSettings(),
                              builder: (BuildContext context,
                                  AsyncSnapshot snapshot) {
                                final countries = snapshot.data;
                                if (snapshot.data == null) {
                                  //IF NULL
                                  return Container(
                                    child: Center(
                                      child: Text("Loading..."),
                                    ),
                                  );
                                } else {
                                  return Stack(
                                    children: [
                                      Container(
                                          child: Text(snapshot.data.Car[0].id)
                                      ),
                                     ),
                                    ],
                                  );
                                }
                              },
                            ),

JSON数据就是这样。

{
    "array": {
        "car": [
            {
                "id": 131,
                "name": "120",
                "number_kms_min": "1000",
                "number_kms_max": "10000",
                "price_min": "10000",
                "price_max": "1000",
                "power_horse_min": "100",
                "power_horse_max": "120",
                "status": "1",
                "is_saved": 0,
                "mark_id": 1,
                "mark_name": "BMW",
                "mark_image_url": "https://cdn0.iconfinder.com/data/icons/car-brands/550/BMW_logo-512.png",
                "model_id": 1,
                "model_name": "Serie 1",
                "model_image_url": "https://www.bmw.pt/content/dam/bmw/common/all-models/1-series/5-door/2019/navigation/bmw-1-series-modelfinder.png",
                "bodywork_id": 1,
                "bodywork_name": "Coupé",
                "fuel_id": 1,
                "fuel_name": "Gasolina",
                "Motorization_id": 1,
                "Motorization_name": "Manual",
                "rims_id": 1,
                "rims_name": "Preto fosco\r\n5 raios",
                "serie_id": 1,
                "serie_name": "120",
                "Interior_equipment_id": 1,
                "Interior_equipment_name": "Bancos desportivos\r\n",
                "Upholstery_id": 1,
                "Upholstery_name": "Preto",
                "Upholstery_Leather_fabric_name": "Preto",
                "ilumination_id": 1,
                "ilumination_name": "Iluminação ambiente",
                "external_equipment_id": 1,
                "external_equipment_name": "Para-choques desportivos",
                "date_start_min": "03/18",
                "date_end_max": "09/19"
            },
            {
                "id": 121,
                "name": "120",
                "number_kms_min": "1000",
                "number_kms_max": "10000",
                "price_min": "10000",
                "price_max": "1000",
                "power_horse_min": "100",
                "power_horse_max": "120",
                "status": "1",
                "is_saved": 0,
                "mark_id": 1,
                "mark_name": "BMW",
                "mark_image_url": "https://cdn0.iconfinder.com/data/icons/car-brands/550/BMW_logo-512.png",
                "model_id": 1,
                "model_name": "Serie 1",
                "model_image_url": "https://www.bmw.pt/content/dam/bmw/common/all-models/1-series/5-door/2019/navigation/bmw-1-series-modelfinder.png",
                "bodywork_id": 1,
                "bodywork_name": "Coupé",
                "fuel_id": 1,
                "fuel_name": "Gasolina",
                "Motorization_id": 1,
                "Motorization_name": "Manual",
                "rims_id": 1,
                "rims_name": "Preto fosco\r\n5 raios",
                "serie_id": 1,
                "serie_name": "120",
                "Interior_equipment_id": 1,
                "Interior_equipment_name": "Bancos desportivos\r\n",
                "Upholstery_id": 1,
                "Upholstery_name": "Preto",
                "Upholstery_Leather_fabric_name": "Preto",
                "ilumination_id": 1,
                "ilumination_name": "Iluminação ambiente",
                "external_equipment_id": 1,
                "external_equipment_name": "Para-choques desportivos",
                "date_start_min": "03/18",
                "date_end_max": "09/19"
            },
            {
                "id": 111,
                "name": "120",
                "number_kms_min": "1000",
                "number_kms_max": "10000",
                "price_min": "10000",
                "price_max": "1000",
                "power_horse_min": "100",
                "power_horse_max": "120",
                "status": "1",
                "is_saved": 0,
                "mark_id": 1,
                "mark_name": "BMW",
                "mark_image_url": "https://cdn0.iconfinder.com/data/icons/car-brands/550/BMW_logo-512.png",
                "model_id": 1,
                "model_name": "Serie 1",
                "model_image_url": "https://www.bmw.pt/content/dam/bmw/common/all-models/1-series/5-door/2019/navigation/bmw-1-series-modelfinder.png",
                "bodywork_id": 1,
                "bodywork_name": "Coupé",
                "fuel_id": 1,
                "fuel_name": "Gasolina",
                "Motorization_id": 1,
                "Motorization_name": "Manual",
                "rims_id": 1,
                "rims_name": "Preto fosco\r\n5 raios",
                "serie_id": 1,
                "serie_name": "120",
                "Interior_equipment_id": 1,
                "Interior_equipment_name": "Bancos desportivos\r\n",
                "Upholstery_id": 1,
                "Upholstery_name": "Preto",
                "Upholstery_Leather_fabric_name": "Preto",
                "ilumination_id": 1,
                "ilumination_name": "Iluminação ambiente",
                "external_equipment_id": 1,
                "external_equipment_name": "Para-choques desportivos",
                "date_start_min": "03/18",
                "date_end_max": "09/19"
            }
        ]
    }
}
    

简单的JSON数据获取并在屏幕上显示,数据正在打印中显示,但是在屏幕上显示NoSuchMethodErrorClass'ResponsiveObjectSettingByUser'的给定错误时,没有实例方法'...

json flutter dart flutter-layout
1个回答
0
投票

您可以在下面复制粘贴运行完整代码您可以使用AsyncSnapshot<ResponseObjectSettingByUser>并在connectionState中选中snapshot.data.array.car.length并使用ListView

© www.soinside.com 2019 - 2024. All rights reserved.