【发布时间】:2022-01-11 09:41:07
【问题描述】:
我目前正在构建一个应用程序来通过 api 读取数据,并且我正在尝试从 JSON Placeholder 解析 JSON api,并且我不断收到 abpve 错误,以防有人知道我非常感谢它。
这是应用程序的模型
class MachineModel {
int id;
String machine_name;
String machine_number_plate;
double location_lat;
double location_lng;
MachineModel({
int id,
String machine_name,
String machine_number_plate,
double location_lat,
double location_lng,
}) {
this.id = id;
this.machine_name = machine_name;
this.machine_number_plate = machine_number_plate;
this.location_lat = location_lat;
this.location_lng = location_lng;
}
MachineModel.fromJson(Map<String, dynamic> json) {
this.id = json['id'];
this.machine_name = json['machine_name'];
this.machine_number_plate = json['machine_number_plate'];
this.location_lat = json['location_lat'];
this.location_lng = json['location_lon'];
}
Map<String, dynamic> toJson() => {
'id': this.id,
'machine_name': this.machine_name,
'machine_number_plate': this.machine_number_plate,
'location_lat': this.location_lat,
'location_lon': this.location_lng
};
}
这是应用程序的回购
class MachineRepo {
final DioClient dioClient;
MachineRepo({@required this.dioClient});
Future<ApiResponse> getMachines() async {
try {
Response response = await dioClient.get(
AppConstants.GET_MACHINE_LIST_WAILON,
);
return ApiResponse.withSuccess(response);
} catch (e) {
return ApiResponse.withError(ApiErrorHandler.getMessage(e));
}
}
这是应用程序的提供者
class MachineProvider with ChangeNotifier {
final MachineRepo machineRepo;
MachineProvider({@required this.machineRepo});
bool _isLoading = false;
List<MachineModel> machines = [];
List<MachineModel> machines_ = [];
bool get isLoading => _isLoading;
Future getMachines() async {
_isLoading = true;
ApiResponse apiResponse = await machineRepo.getMachines();
print(apiResponse);
_isLoading = false;
if (apiResponse.response != null &&
apiResponse.response.statusCode == 200) {
Map map_ = apiResponse.response.data;
//TODO: convert API response to MachineModal List
machines = map_['machines']
.map<MachineModel>((machine) => MachineModel.fromJson(machine))
.toList();
machines_ = machines;
print(machines);
notifyListeners();
} else {
String errorMessage;
if (apiResponse.error is String) {
print(apiResponse.error.toString());
errorMessage = apiResponse.error.toString();
} else {
ErrorResponse errorResponse = apiResponse.error;
print(errorResponse.errors[0].message);
errorMessage = errorResponse.errors[0].message;
}
notifyListeners();
}
}
json
[
{
"id": 34406000,
"machine_name": "Bomag race care .",
"machine_number_plate": "",
"location_lat": 0.922445,
"location_lon": 34.313638
},
{
"id": 244218282,
"machine_name": "Bus test",
"machine_number_plate": "",
"location_lat": -0.883203,
"location_lon": 50.66626
},
{
"id": 24444459,
"machine_name": "Test Car",
"machine_number_plate": "",
"location_lat": 0.2441445,
"location_lon": 32.577147
}
]
任何帮助
【问题讨论】:
-
请提供您的错误回溯