2
Future userPasswordUpdate() async {

    String passwordU = password.text;
    String confirmPasswordU = confirmPassword.text;
    String oldPasswordU = oldPassword.text;

    var url = 'url';

    var response = await http.put(url,
        headers: {
          'Accept': 'application/json'
        },
        body: {
          "password": passwordU,
          "confirmPass": confirmPasswordU,
          "oldpassword": oldPasswordU,
        }
    );

I want to post image file to server with this method. But I Don't Know How. Can Anyone Help Me ?

1
  • Hi @Magatha ,please check the below solution andd let me know in case of concern Commented Apr 15, 2020 at 6:26

1 Answer 1

1

For the image upload you can also use the Dio library to post image on server with requested parameters.Please check the below example of it.

Dio dio = new Dio(); // with default Options

// Set default configs
    dio.options.baseUrl = BASE_URL;
    dio.options.connectTimeout = 5000; //5s
    dio.options.receiveTimeout = 3000;
    dio.options.headers[HEADER_AUTH_TOKEN_KEY] = HEADER_AUTH_TOKEN_VALUE;
    dio.options.headers[HEADER_VERSION_KEY] = HEADER_VERSION_VALUE;



    FormData formData = new FormData.fromMap({
      "password": passwordU,
      "confirmPass": confirmPasswordU,
      "oldpassword": oldPasswordU,


      "YOUR_IMAGE_PARAMETER_NAME": await MultipartFile.fromFile(imageFile.path,filename: imageFile.path.split("/").last),

    });

    var response = await dio.post(REGISTRATION_URL, data: formData);

    if (response.statusCode == 200) {
      apiResponse.onSuccess(response.toString(), eventType);
      print("Image Uploaded");
    } else {
      apiResponse.onError('Failed to load post');
      print("Upload Failed");
    }
  }

Inside the pubspec.yaml used this library,

dio: ^3.0.9

For the info about the library , you need to check this link Click

Sign up to request clarification or add additional context in comments.

4 Comments

Still Im Getting Error Like Unhandled Exception: DioError [DioErrorType.RESPONSE]: Http status error [422]
I have updated the solution, you need to take care of bothBASE_URL and REGISTRATION_URL like in your base url there is only url like http//www.xxxxxx.com and in your REGISTRATION_URL there is futher name like "updateProfile.php"
Thank You, I Think It's Worked. Thank You Soo Much
@Magtha, happy to help you..keep happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.