How to convert dio response to json/map in flutter

Viewed 24

I am trying to insert the data into the database. After inserted successfullly i want to show the response msg to the user but in dio there is no method to convert response to jsonDecode something. I searched and I found this method. Response and Map

 static createNewSupervisor(String name, String email, String address,
      String site, String mobileNumber, String password, File? image) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var token = prefs.getString("token");
    var dio = Dio();
    var fd = FormData.fromMap({
      "full_name": name,
      "email": email,
      "password": password,
      "image": image == null ? "" : await MultipartFile.fromFile(image.path),
    });
    Response response = await dio.post(
      "$baseURL/api/",
      data: fd,
      options: Options(
        responseType: ResponseType.plain,
        headers: {
          'Authorization': 'Bearer $token',
        },
      ),
    );
    Map responseBody = response;
// want to convert to the JSON here
    return responseBody;
  }

response

{
data : {...object},
msg: "Data inserted successfully",
}

**snackBar code **

AdminSupervisorServices.createNewSupervisor(
                                    _nameController.text,
                                   //... rest code
                                    uploadimage)
                                .then((response) {
                              print(response['data']['msg']);
                              ScaffoldMessenger.of(context).showSnackBar(
                                SnackBar(
                                  behavior: SnackBarBehavior.floating,
                                  content: Row(
                                    children: const [
                                      Icon(FeatherIcons.home),
                                      SizedBox(
                                        width: 10,
                                      ),
                                      Text(response['data']['msg']),
                                    ],
                                  ),
                                ),
0 Answers
Related