Post 序列化 Json body - [VERBOSE-2:ui_dart_state.cc(177)] 未处理的异常:错误-
Post serialised Json body - [VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: Error-
过去 2 天我一直在思考关于 JSON 行 body 的 flutter post 方法,这里显示了我尝试过的代码。
代码 1:
void send() async {
userModel = UserModel(userName: "usernme", password: "password");
var response = await http.post(
"https://stagingapi......",
headers: {"Content-Type": "application/json"},
body: json.encode(userModel.toJson()));
print(response.body);
print(response.statusCode);
}
我创建了序列化的 body 模型,
import 'package:json_annotation/json_annotation.dart';
part 'UserModel.g.dart';
@JsonSerializable()
class UserModel {
String userName;
String password;
// String xyz;
UserModel({this.userName, this.password});
factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);
Map<String, dynamic> toJson() => _$UserModelToJson(this);
}
代码 2:-
Future<User> createUser(String username, String password) async{
final String apiUrl = "https://stagingapi.....";
Map<String,String> headers = {'Content-Type':'application/json'};
final body = jsonEncode({ "userName": username,
"password": password});
try {
var response = await http.post(apiUrl, headers: headers, body: body);
print(response.statusCode);
if(response.statusCode == 201){
final String responseString = response.body;
return userFromJson(responseString);
}else{
return null;
}
} catch (error) {
print('error- $error');
}
}
}
在这两种情况下,或者如果我尝试任何 post 代码方法,它都会抛出以下错误,
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception:
#0 IOClient.send (package:http/src/io_client.dart:65:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
#2 BaseClient.post (package:http/src/base_client.dart:58:7)
#3 post.<anonymous closure> (package:http/http.dart:70:16)
#4 _withClient (package:http/http.dart:166:20)
#5 post (package:http/http.dart:69:5)
#6 _SelectDOBState.send (package:kash_app/SelectDOB.dart:99:29)
#7 _SelectDOBState.initState (package:kash_app/SelectDOB.dart:92:6)
#8 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4812:57)
#9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4649:5)
#10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
#11 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
#12 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:616<…>
以上代码在其他 url 中有效...我已经解决了这个问题,这是我们的后端团队通过添加一些 [=10= 对域 url 进行了一些更改]
过去 2 天我一直在思考关于 JSON 行 body 的 flutter post 方法,这里显示了我尝试过的代码。
代码 1:
void send() async {
userModel = UserModel(userName: "usernme", password: "password");
var response = await http.post(
"https://stagingapi......",
headers: {"Content-Type": "application/json"},
body: json.encode(userModel.toJson()));
print(response.body);
print(response.statusCode);
}
我创建了序列化的 body 模型,
import 'package:json_annotation/json_annotation.dart';
part 'UserModel.g.dart';
@JsonSerializable()
class UserModel {
String userName;
String password;
// String xyz;
UserModel({this.userName, this.password});
factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);
Map<String, dynamic> toJson() => _$UserModelToJson(this);
}
代码 2:-
Future<User> createUser(String username, String password) async{
final String apiUrl = "https://stagingapi.....";
Map<String,String> headers = {'Content-Type':'application/json'};
final body = jsonEncode({ "userName": username,
"password": password});
try {
var response = await http.post(apiUrl, headers: headers, body: body);
print(response.statusCode);
if(response.statusCode == 201){
final String responseString = response.body;
return userFromJson(responseString);
}else{
return null;
}
} catch (error) {
print('error- $error');
}
}
}
在这两种情况下,或者如果我尝试任何 post 代码方法,它都会抛出以下错误,
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception:
#0 IOClient.send (package:http/src/io_client.dart:65:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
#2 BaseClient.post (package:http/src/base_client.dart:58:7)
#3 post.<anonymous closure> (package:http/http.dart:70:16)
#4 _withClient (package:http/http.dart:166:20)
#5 post (package:http/http.dart:69:5)
#6 _SelectDOBState.send (package:kash_app/SelectDOB.dart:99:29)
#7 _SelectDOBState.initState (package:kash_app/SelectDOB.dart:92:6)
#8 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4812:57)
#9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4649:5)
#10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
#11 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
#12 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:616<…>
以上代码在其他 url 中有效...我已经解决了这个问题,这是我们的后端团队通过添加一些 [=10= 对域 url 进行了一些更改]