Created
July 5, 2021 20:08
-
-
Save blisssan/d9e82e61237e42e648d7980dfbb0dc0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
Future<dynamic> dioPostFormData( | |
String url, { | |
Map<String, String> headers, | |
Map<String, dynamic> body, | |
String method = 'POST', | |
Map<String, Uint8List> files, | |
}) async { | |
FormData formData; | |
// this will be for regular content other than file | |
formData = FormData.fromMap(body); | |
if (files != null) { | |
for (MapEntry<String, Uint8List> entry in files.entries) { | |
MapEntry<String, MultipartFile> mapEntry = MapEntry( | |
entry.key, | |
MultipartFile.fromBytes( | |
uint8list, | |
), | |
); | |
formData.files.add(mapEntry); | |
} | |
} | |
Dio dio = new Dio(BaseOptions( | |
headers: headers, | |
receiveTimeout: 60 * 1000, | |
sendTimeout: 60 * 1000, | |
)); | |
var response; | |
response = await dio | |
.post( | |
url, | |
data: formData, | |
); | |
return response.data; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
Future<dynamic> dioPostFormData( | |
String url, { | |
Map<String, String> headers, | |
Map<String, dynamic> body, | |
String method = 'POST', | |
Map<String, File> files, | |
}) async { | |
FormData formData; | |
// this will be for regular content other than file | |
// if you dont have any extra data simply pass an empty map like <String,dynamic>{} for body field. | |
formData = FormData.fromMap(body); | |
if (files != null) { | |
for (MapEntry<String, File> entry in files.entries) { | |
MapEntry<String, MultipartFile> mapEntry = MapEntry( | |
entry.key, | |
// in this example I used a file object, to use the Uint8list you can use MultipartFile.fromBytes method | |
// i have modified it below in another example so you can use that and see | |
await MultipartFile.fromFile( | |
entry.value.path, | |
filename: basename(entry.value.path), | |
), | |
); | |
formData.files.add(mapEntry); | |
} | |
} | |
Dio dio = new Dio(BaseOptions( | |
headers: headers, | |
receiveTimeout: 60 * 1000, | |
sendTimeout: 60 * 1000, | |
)); | |
var response; | |
response = await dio | |
.post( | |
url, | |
data: formData, | |
); | |
return response.data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment