我正在使用 flutter Retrofit 包,尝试将数据上传到服务器(Laravel)。基本上,文件应作为 MultiPart 部分注释发送。另外,我应该与文件(在我的例子中是图片)一起发送不同的值,所以我使用了@Part()map<String,dynamic>。
这是我的代码
@POST('register')
Future<RegisterResponse> register(@Body() Map<String, dynamic> body, @Part() File pic);
Run Code Online (Sandbox Code Playgroud)
这是我的要求
Map<String, dynamic> body = {
"name":" widget.fullName",
"username": "widget.userName",
"phone": "698281556",
"country_code": "+213",
"email": "widget.email@gmail.com",
"birth_date": "18-09-2021",
"pic": imageFile,
"lat": 36.347285,
"lon": 6.603381,
"address": "widget.userName",
"city": "widget.userName",
"residential": "widget.userName",
"device_token": "widget.userName",
"code": "123456",
"tags": [1,2]
};
final logger = Logger();
final dio = Dio(); // Provide a dio instance
dio.options.headers["Content-Type"] =
"multipart/form-data";
dio.options.headers["X-Requested-With"] =
"XMLHttpRequest";
final client = RestClient(dio);
client.register(body,imageFile!).then((it) async {
print(it.access_token); …Run Code Online (Sandbox Code Playgroud)