英文:
_TypeError (type '(dynamic) => User' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform')
问题
I'm here to provide translations. Here's the translated content:
我是Flutter的新手,正在按照教程链接操作。我遇到了TypeError错误,这里我使用了http
库来获取我使用Node Express创建的数据。
如果我使用jsonplaceholder的虚拟API,它可以工作,但在我的API中会出现错误,为什么呢?
用户模型
import 'dart:convert';
List<User> userFromJson(String str) =>
List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
String userToJson(List<User> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class User {
int index;
int id;
String firstName;
String lastName;
String phone;
String email;
User({
required this.index,
required this.id,
required this.firstName,
required this.lastName,
required this.phone,
required this.email,
});
factory User.fromJson(Map<String, dynamic> json) => User(
index: json["index"],
id: json["id"],
firstName: json["first_name"],
lastName: json["last_name"],
phone: json["phone"],
email: json["email"],
);
Map<String, dynamic> toJson() => {
"index": index,
"id": id,
"first_name": firstName,
"last_name": lastName,
"phone": phone,
"email": email,
};
}
HTTP的GET方法
import 'package:http/http.dart' as http;
const String baseUrl = "http://192.168.166.153:5000/api/user";
class BaseClient {
var client = http.Client();
Future<dynamic> get(String api) async {
var url = Uri.parse(baseUrl + api);
var headers = {
"authorization":
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InNzZ2dAbW0uaW4iLCJpYXQiOjE2ODQ2NzEwMDEsImV4cCI6MTY4NDcyNTAwMX0.lTIIgHi7129dxvoI83sN82r5sHG88E8RGL2nsEimD1I",
};
var response = await client.get(url, headers: headers);
if (response.statusCode == 200) {
return response.body;
} else {
// 其他情况
}
}
}
从REST API接收到的响应
{
"responseCode": 200,
"responseMessage": "一切都按预期进行",
"responseData": [
{
"index": 2,
"id": 1,
"first_name": "oggy",
"last_name": "ss",
"phone": "1234567890",
"email": "ss@ss.in"
},
{
"index": 3,
"id": 2,
"first_name": "olly",
"last_name": "ss",
"phone": "1234567899",
"email": "gg@gg.in"
},
{
"index": 4,
"id": 3,
"first_name": "jack",
"last_name": "jj",
"phone": "1234567898",
"email": "jj@jj.in"
}
]
}
英文:
I'm new to Flutter and following the tutorial link. and I get the TypeError, here I have used http
library to fetch the data that I have created using node express.
if I use fake API from jsonplaceholder it works but get an error in my own API why?
user model
import 'dart:convert';
List<User> userFromJson(String str) =>
List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
String userToJson(List<User> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class User {
int index;
int id;
String firstName;
String lastName;
String phone;
String email;
User({
required this.index,
required this.id,
required this.firstName,
required this.lastName,
required this.phone,
required this.email,
});
factory User.fromJson(Map<String, dynamic> json) => User(
index: json["index"],
id: json["id"],
firstName: json["first_name"],
lastName: json["last_name"],
phone: json["phone"],
email: json["email"],
);
Map<String, dynamic> toJson() => {
"index": index,
"id": id,
"first_name": firstName,
"last_name": lastName,
"phone": phone,
"email": email,
};
}
HTTP get method
import 'package:http/http.dart' as http;
const String baseUrl = "http://192.168.166.153:5000/api/user";
class BaseClient {
var client = http.Client();
Future<dynamic> get(String api) async {
var url = Uri.parse(baseUrl + api);
var headers = {
"autherization":
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InNzZ2dAbW0uaW4iLCJpYXQiOjE2ODQ2NzEwMDEsImV4cCI6MTY4NDcyNTAwMX0.lTIIgHi7129dxvoI83sN82r5sHG88E8RGL2nsEimD1I",
};
var response = await client.get(url, headers: headers);
if (response.statusCode == 200) {
return response.body;
} else {
// else part
}
}
}
response received form REST API
{
"responseCode": 200,
"responseMessage": "Everything worked as expected",
"responseData": [
{
"index": 2,
"id": 1,
"first_name": "oggy",
"last_name": "ss",
"phone": "1234567890",
"email": "ss@ss.in"
},
{
"index": 3,
"id": 2,
"first_name": "olly",
"last_name": "ss",
"phone": "1234567899",
"email": "gg@gg.in"
},
{
"index": 4,
"id": 3,
"first_name": "jack",
"last_name": "jj",
"phone": "1234567898",
"email": "jj@jj.in"
}
]
}
答案1
得分: 1
将以下内容翻译为中文:
"Your JSON doesn't just include the raw array. The array you want is contained inside a JSON object that also contains the result code.
Change:
List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
to:
List<User>.from(json.decode(str)['responseData'].map((x) => User.fromJson(x)));"
请注意,代码部分未被翻译。
英文:
Your JSON doesn't just include the raw array. The array you want is contained inside a JSON object that also contains the result code.
Change:
List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
to:
List<User>.from(json.decode(str)['responseData'].map((x) => User.fromJson(x)));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论