Flutter 将数据从颤振发布到API中
我有一个API,它是在Flutter 将数据从颤振发布到API中,flutter,Flutter,我有一个API,它是在Node.js中编写的。它用于记录日志。现在我正在使用颤振应用程序中的API来执行相同的操作 API : http://192.168.137.1:5050/User/Login 如何将数据从颤振应用程序发布到API Expected Input : { Data: { USER_NAME: 'usenmae', PASSWORD: 'password' } } 1.从颤振 static Future<String> loginPost(String
Node.js
中编写的。它用于记录日志。现在我正在使用颤振应用程序中的API来执行相同的操作
API : http://192.168.137.1:5050/User/Login
如何将数据从颤振应用程序发布到API
Expected Input :
{ Data: { USER_NAME: 'usenmae', PASSWORD: 'password' } }
1.从颤振
static Future<String> loginPost(String email, String password) async {
Map<String, dynamic> jsonMap = {
'Data': {'USER_NAME': email,'PASSWORD' : password}
};
try {
final response = await http.post(
'http://192.168.137.1:5050/User/Login',
body: jsonMap,
);
return response.body;
} catch (exception) {
}
return null;
}
static Future loginPost(字符串电子邮件、字符串密码)异步{
映射jsonMap={
'Data':{'USER_NAME':电子邮件,'PASSWORD':PASSWORD}
};
试一试{
最终响应=等待http.post(
'http://192.168.137.1:5050/User/Login',
正文:jsonMap,
);
返回响应.body;
}捕获(例外){
}
返回null;
}
在使用上述代码片段调用API之后,我遇到了一些错误
Error : I/flutter ( 4149): send message exception called
I/flutter ( 4149): type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast
错误:I/flatter(4149):调用发送消息异常
I/flatter(4149):类型“\u InternalLinkedHashMap”不是类型转换中“String”类型的子类型
我想通过api传递用户名和密码,方法是用另一个数据对象包装用户名和密码,看起来像{Data:{USER\u NAME:'usenmae',password:'password'}
谢谢在将数据传递到http.post()之前,请使用dart:convert对jsonMap数据进行编码
import 'dart:convert' as convert;
final response = await http.post(
'http://192.168.137.1:5050/User/Login',
body: convert.jsonEncode(jsonMap),
headers: {"Content-type" : "application/json"}
);
只需要像前面所说的那样编码为json,并将其放入标题“内容类型”:“application/json”尝试通过使用
jsonecode
编码将数据传递到API。但传递的数据(如用户名和密码)没有进入API。在节点js
的API端打印req.body
时返回null<代码>控制台日志(请求正文)代码>返回空值