我有一个问题,我在
data
的 post
请求中传递的任何内容都没有被接收或为空。但我检查并打印出来,它有值。我尝试了邮递员中的链接,但请求顺利通过。我尝试了很多方法,比如 dio
json.encode,
、jsonEncode
,甚至只是像 FormData.fromMap()
这样的手动和硬编码数据输入方式。我在 http 请求中使用了相同的 toJson() 方法并且它工作正常但是当我在 dio 中使用它时它返回错误 400这里是 dio 函数:
{'first_name': 'sample',...}
以下是我如何使用该功能:
Future<User?> registerUser({required User user}) async {
User? retrievedUser;
try {
Response response = await _dio.post(
'http://localhost/api/v1/index.php/registration',
data: user.toJson(), // THIS RETURNS STATUS CODE 400 AND IS SAID TO BE NULL
// I TRIED:
// user.toJson()
// jsonEncode(user.toJson())
// json.encode(user.toJson())
// FormData.fromMap(user.toJson())
// FormData.fromMap(json.encode(user.toJson()))
// I tried even the manual way like {'first_name': 'sample',...}
// i also enclosed it in json encode but still the same error
);
print('User created: ${response.data}');
retrievedUser = User.fromJson(response.data);
} catch (e) {
print('Error creating user: $e');
}
return retrievedUser;
}
有关额外信息,这里是我现在使用的模型,它与 JsonSerializable 一起使用,但它仍然对我不起作用:
FilledButton(
onPressed: () async {
User userInfo = User(
firstName: firstNameController.text,
lastName: lastNameController.text,
birthday: birthdayController.text,
gender: genderController.text,
mobile: mobileController.text,
);
User? retrievedUser = await registerUser(user: userInfo);
print(retrievedUser);
// if (_formKey.currentState!.validate()) {
// }
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => const Login(),
// ),
// );
},
child: const Text(
'Register',
),
),
这是 api 文档:
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User {
String firstName;
String lastName;
String birthday;
String gender;
String mobile;
User(
{required this.firstName,
required this.lastName,
required this.birthday,
required this.gender,
required this.mobile});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'user.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
User _$UserFromJson(Map<String, dynamic> json) => User(
firstName: json['first_name'] as String,
lastName: json['last_name'] as String,
birthday: json['birthday'] as String,
gender: json['gender'] as String,
mobile: json['mobile'] as String,
);
Map<String, dynamic> _$UserToJson(User instance) => <String, dynamic>{
'first_name': instance.firstName,
'last_name': instance.lastName,
'birthday': instance.birthday,
'gender': instance.gender,
'mobile': instance.mobile,
};
这里是工作邮递员要求
我使用dio函数的方式或我传递数据的方式有什么问题吗?我尝试在有和没有 jsonEncode 的情况下打印 user.toJson() ,数据很好,也以 json 形式显示。我找不到我的代码有什么问题。
最后的附加信息,这是我以前使用的模型,它适用于 http post 请求,但在 dio 中使用时不起作用:
$app->post('/registration', function () use ($app){
//Required Parameters
verifyRequiredParams(array('first_name','last_name','birthday','gender','mobile'));
//includes
$database = new Database();
$conn = $database->getConnection();
//
$db = new SaveInfo($conn);
$response = array();
$first_name = $app->request->post('first_name');
$last_name = $app->request->post('last_name');
$birthday = $app->request->post('birthday');
$gender = $app->request->post('gender');
$mobile = $app->request->post('mobile');
$result = $db->savePersonalInfo($first_name,$last_name,$birthday,$gender,$mobile);
if($result){
$response['error'] = false;
$response['message'] = "Successfully Registered";
}
else{
$response['error'] = true;
$response['message'] = "Failed to register";
}
echoResponse(200,$response);
});
这是 http post 请求
import 'package:json_annotation/json_annotation.dart';
@JsonSerializable()
class RegistrationInfo {
String? firstName;
String? lastName;
String? birthday;
String? gender;
String? mobile;
RegistrationInfo(
{required this.firstName,
required this.lastName,
required this.birthday,
required this.gender,
required this.mobile});
RegistrationInfo.fromJson(Map<String, dynamic> json) {
firstName = json['first_name'];
lastName = json['last_name'];
birthday = json['birthday'];
gender = json['gender'];
mobile = json['mobile'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['first_name'] = firstName;
data['last_name'] = lastName;
data['birthday'] = birthday;
data['gender'] = gender;
data['mobile'] = mobile;
return data;
}
}
这是我的注册屏幕的全部代码:
Future<int> checkUser(String firstName, String lastName) async {
final url = Uri.parse(
'http://localhost/api/v1/index.php/checkUserExist?first_name=$firstName&last_name=$lastName');
try {
final response = await http.get(url);
userInfo = jsonDecode(response.body);
return response.statusCode;
} catch (e) {
debugPrint(e.toString());
return 400;
}
}