我无法创建其中包含另一张地图的地图
I am unable to create a map that has another map inside it
我正在尝试创建一个注册模型并将其发送到我的 API,但“位置”密钥有另一个 map/jsonobject,其中包含“类型”:“点”和“坐标": [double, double].
最终的 json 对象应该看起来像这样
{
"name": "Arsh Bansal",
"email": "ab@yahoo.com",
"password": "123456789",
"birthday": "06-21-2000",
"gender": "Male",
"location": {
"type": "Point",
"coordinates": [13.0987, 88.403]
},
"phone_number": "123456789"
}
网络助手代码如下:
import 'package:http/http.dart' as http;
import 'dart:convert';
class NetworkHelper {
NetworkHelper(this.url);
NetworkHelper.withHeader(this.url, this.headers);
final String url;
Map<String, String> headers;
Future getData() async {
http.Response response = await http.get(url);
if(response.statusCode == 200) {
String data = response.body;
return jsonDecode(data);
} else {
String error = response.body;
}
}
Future<http.Response> postData(Map body) async {
http.Response response = await http.post(url, headers: headers, body: body);
return response;
}
}
但是我得到以下错误
我得到的错误是:
未处理的异常:类型“_InternalLinkedHashMap”不是类型 cast
中类型 'String' 的子类型
#0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:288:25)
#1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
#2 CastMap.forEach (dart:_internal/cast.dart:287:13)
#3 mapToQuery (package:http/src/utils.dart:17:7)
#4 Request.bodyFields= (package:http/src/request.dart:137:12)
#5 BaseClient._sendUnstreamed (package:http/src/base_client.dart:170:17)
#6 BaseClient.post (package:http/src/base_client.dart:58:7)
#7 post.<anonymous closure> (package:http/http.dart:70:16)
#8 _withClient (package:http/http.dart:166:20)
#9 post (package:http/http.dart:69:5)
#10 NetworkHelper.postData (package:clubinn/services/network_helper.dart:28:36)
#11 Models.createUser (package:clubinn/models/models.dart:72:50)
#12 _SignUpPageState.build.<anonymous closure> (package:clubinn/sc<…>
您的问题是 http.post
和 body
参数的使用。如果您阅读文档,您会看到它被描述为:
body sets the body of the request. It can be a String, a List or a Map. If it's a String, it's encoded using encoding and used as the body of the request. The content-type of the request will default to "text/plain".
...
If body is a Map, it's encoded as form fields using encoding. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.
https://pub.dev/documentation/http/latest/http/post.html
您的输入 body
来自以下代码:
Map<String, dynamic> body = {
"name": name,
"email": email,
"password": password,
"birthday": DOB,
"gender": gender,
"location": location.toJson(),
"phone_number": mobile
};
类型甚至不接近 Map<String, String>
因为例如位置被赋予 Map<String, dynamic>
作为值:
Map<String,dynamic> toJson() => {
"type": type,
"coordinates": List<double>.from(coordinates.map((x) => x)),
};
此外,我认为您一开始就不想给 body
地图。正如您在前面提到的文档中看到的那样,地图是 "encoded as form fields using encoding"。您真正想要的是将地图解析为 JSON 字符串并将该字符串作为正文提供给 http.post
.
所以它应该是这样的:
http.Response response = await http.post(url, headers: headers, body: json.encode(body));
我正在尝试创建一个注册模型并将其发送到我的 API,但“位置”密钥有另一个 map/jsonobject,其中包含“类型”:“点”和“坐标": [double, double].
最终的 json 对象应该看起来像这样
{
"name": "Arsh Bansal",
"email": "ab@yahoo.com",
"password": "123456789",
"birthday": "06-21-2000",
"gender": "Male",
"location": {
"type": "Point",
"coordinates": [13.0987, 88.403]
},
"phone_number": "123456789"
}
网络助手代码如下:
import 'package:http/http.dart' as http;
import 'dart:convert';
class NetworkHelper {
NetworkHelper(this.url);
NetworkHelper.withHeader(this.url, this.headers);
final String url;
Map<String, String> headers;
Future getData() async {
http.Response response = await http.get(url);
if(response.statusCode == 200) {
String data = response.body;
return jsonDecode(data);
} else {
String error = response.body;
}
}
Future<http.Response> postData(Map body) async {
http.Response response = await http.post(url, headers: headers, body: body);
return response;
}
}
但是我得到以下错误
我得到的错误是:
未处理的异常:类型“_InternalLinkedHashMap
#0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:288:25)
#1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
#2 CastMap.forEach (dart:_internal/cast.dart:287:13)
#3 mapToQuery (package:http/src/utils.dart:17:7)
#4 Request.bodyFields= (package:http/src/request.dart:137:12)
#5 BaseClient._sendUnstreamed (package:http/src/base_client.dart:170:17)
#6 BaseClient.post (package:http/src/base_client.dart:58:7)
#7 post.<anonymous closure> (package:http/http.dart:70:16)
#8 _withClient (package:http/http.dart:166:20)
#9 post (package:http/http.dart:69:5)
#10 NetworkHelper.postData (package:clubinn/services/network_helper.dart:28:36)
#11 Models.createUser (package:clubinn/models/models.dart:72:50)
#12 _SignUpPageState.build.<anonymous closure> (package:clubinn/sc<…>
您的问题是 http.post
和 body
参数的使用。如果您阅读文档,您会看到它被描述为:
body sets the body of the request. It can be a String, a List or a Map. If it's a String, it's encoded using encoding and used as the body of the request. The content-type of the request will default to "text/plain".
...
If body is a Map, it's encoded as form fields using encoding. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.
https://pub.dev/documentation/http/latest/http/post.html
您的输入 body
来自以下代码:
Map<String, dynamic> body = {
"name": name,
"email": email,
"password": password,
"birthday": DOB,
"gender": gender,
"location": location.toJson(),
"phone_number": mobile
};
类型甚至不接近 Map<String, String>
因为例如位置被赋予 Map<String, dynamic>
作为值:
Map<String,dynamic> toJson() => {
"type": type,
"coordinates": List<double>.from(coordinates.map((x) => x)),
};
此外,我认为您一开始就不想给 body
地图。正如您在前面提到的文档中看到的那样,地图是 "encoded as form fields using encoding"。您真正想要的是将地图解析为 JSON 字符串并将该字符串作为正文提供给 http.post
.
所以它应该是这样的:
http.Response response = await http.post(url, headers: headers, body: json.encode(body));