Advertisement
LightkingBeknazarov

image upload

Jun 4th, 2022
1,580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.82 KB | None | 0 0
  1. void takePhoto() async {
  2.     final pickedImage =
  3.         await ImagePicker().pickImage(source: ImageSource.camera);
  4.     if (pickedImage != null) {
  5.       profileImagePath = pickedImage.path;
  6.     }
  7.   }
  8.  
  9.  static Future<dynamic> uploadPhoto(String imagePath) async {
  10.     try {
  11.       FormData formData = FormData.fromMap({
  12.         "profile": await MultipartFile.fromFile(imagePath, filename: "profile.jpg")
  13.       });
  14.       Response response = await Dio().post(
  15.         "<url>",
  16.         data: formData,
  17.         options: Options(
  18.           headers: <String, String>{
  19.             'Authorization': 'Bearer <some token>',
  20.           },
  21.         ),
  22.       );
  23.       return response;
  24.     } on DioError catch (e) {
  25.       log(e.response?.data);
  26.       log(e.response?.statusCode);
  27.       return e.response;
  28.     } catch (_) {}
  29.   }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement