gpt4 book ai didi

json - 无法使用 Flutter 项目将 JSON 映射到 Dart 中的列表

转载 作者:行者123 更新时间:2023-12-03 02:49:52 25 4
gpt4 key购买 nike

我正在运行 Flutter 1.0.0(第一个版本),最近升级到 1.2.1,有很多错误和警告需要我纠正。主要是指定注释类型。更正所有这些后,我运行了我的 Android 应用程序,现在将 JSON 映射到列表的代码不起作用。首先,我将发布我的原始代码。 JSON 数据来自 HTTP 请求。

Dart

Future<List<Device>> getDevices() async {
var response = await _httpNetwork.get(_devicesUrl, headers: {"Cookie": _sessionId});

if (response.statusCode < 200 || response.statusCode > 400) {
throw Exception("Error while fetching data");
}

final body = json.decode(response.body);

return body.map<Device>((device) => Device.fromJson(device)).toList();
}

设备.dart
class Device {
Device({
this.id,
this.name,
this.uniqueId,
this.status,
this.phone,
this.model,
this.disabled,
});

factory Device.fromJson(Map<String, dynamic> json) => Device(
id: json['id'],
name: json['name'],
uniqueId: json['uniqueId'],
status: json['status'] == 'online' ? true : false,
phone: json['phone'],
model: json['model'],
disabled: json['disabled'],
);

// API data
int id;
String name;
String uniqueId;
bool status;
String phone;
String model;
bool disabled;

Map<String, dynamic> toJson() => <String, dynamic>{
'id': id,
'name': name,
'uniqueId': uniqueId,
'status': status == true ? 'online' : 'offline',
'phone': phone,
'model': model,
'disabled': disabled,
};
}

现在这里是 api.dart 中的以下更改出现问题的地方.
return json.decode(response.body).map<Device>((Map<String, dynamic> device) => Device.fromJson(device)).toList();

虽然这个语法根据 Android Studio/Flutter/Dart 是正确的,但它似乎不起作用。该应用程序不会崩溃,我也不会在运行控制台中出错,它只是点击了我的 onError我的 Observable<bool>.fromFuture() 中的代码称呼。

我把 print调用我的代码来确定 api.dart 中的返回语句是罪魁祸首。任何人都对这个问题有任何见解?

最佳答案

试试这种方式为响应数据制作 pojo 类..

class UserData {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;

UserData({this.albumId, this.id, this.title, this.url, this.thumbnailUrl});

factory UserData.fromJson(Map<String, dynamic> json) {
return new UserData(
albumId: json['albumId'],
id: json['id'],
title: json['title'],
url: json['url'],
thumbnailUrl: json['thumbnailUrl']);
}
}

获取数据的make方法..
Future<UserData> fetchData() async {
var result = await get('https://jsonplaceholder.typicode.com/photos');

if (result.statusCode == 200) {
return UserData.fromJson(json.decode(result.body));
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post');
}
}

现在像这样制作列表对象..
 Future<UserData> userDataList;

点击按钮..
            userDataList = fetchData();

您也可以使用下面的代码来获取数据列表。
List<UserData> list = List();
var isLoading = false;

void fetchData() async {
setState(() {
isLoading = true;
});
final response = await get("https://jsonplaceholder.typicode.com/photos");
if (response.statusCode == 200) {
list = (json.decode(response.body) as List)
.map((data) => UserData.fromJson(data))
.toList();
setState(() {
isLoading = false;
});
} else {
throw Exception('Failed to load photos');
}
}

关于json - 无法使用 Flutter 项目将 JSON 映射到 Dart 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com