-2

I have tried to implement image_picker with Getx, but didn't succeed.

Getx Version is 4.3.8 Image Picker version is 0.8.4+4

Here is my [pubspec.yaml][1]

Here is my [home_view.dart][2]

Here is my [home_controller.dart][3]

issue: When I select image the app crashes.

  [1]: https://pastebin.com/ZvVHVNaY
  [2]: https://pastebin.com/TFVu42mK
  [3]: https://pastebin.com/kcQXsjEe

For Full code please visit pastebin.

Here is some part of code. home_view.dart

 body: Column(
          children: [
            Expanded(
                child:
                    !kIsWeb && defaultTargetPlatform == TargetPlatform.android
                        ? FutureBuilder<void>(
                            future: c.retrieveLostData(),
                            builder: (BuildContext context,
                                AsyncSnapshot<void> snapshot) {
                              switch (snapshot.connectionState) {
                                case ConnectionState.none:
                                case ConnectionState.waiting:
                                  return Text("No Data");
                                case ConnectionState.done:
                                  return c.returnData();
                                default:
                                  if (snapshot.hasError) {
                                    return Text(
                                      'Pick image/video error: ${snapshot.error}}',
                                      textAlign: TextAlign.center,
                                    );
                                  } else {
                                    return const Text(
                                      'You have not yet picked an image.',
                                      textAlign: TextAlign.center,
                                    );
                                  }
                              }
                            })
                        : c.returnData()),
            TextButton(
                onPressed: () async {
                  await c.fetchImage();
                },
                child: Text("Fetch Image"))

home_controller.dart

class HomeController extends GetxController {
  ImagePicker picker = ImagePicker();
  final selectedPath = "".obs;
  final file = Rx<XFile?>(null);
  final retrieveDataError = "".obs;
  final pickImageError = "".obs;
  Future<void> retrieveLostData() async {
    final LostDataResponse response = await picker.retrieveLostData();
    if (response.isEmpty) {
      return;
    }
    if (response.file != null) {
      file.value = response.file;
      print("second retrived");
    } else {
      retrieveDataError.value = response.exception!.code;
    }
  }
 
  returnData() {
    if (pickImageError.value == "" && retrieveDataError.value == "") {
      return Text("No Data");
    } else if (file.value != null) {
      return Image.file(File(file.value!.path));
    } else {
      retrieveDataError();
      return CircularProgressIndicator();
    }
  }
 
  fetchImage() async {
    try {
      final pickedFile = await picker.pickImage(source: ImageSource.camera);
      if (pickedFile != null) {
        file.value = pickedFile;
        print("first picked");
      } else {
        retrieveLostData();
        print("first retrived");
      }
    } catch (e) {
      pickImageError.value = e.toString();
    }
  }
 
}

I have tried to implement the example given in the package but using getx.

Without Getx, it is working fine.

Please help me to find what I am missing?

Sumit Kumar
  • 678
  • 4
  • 19

1 Answers1

0

home_controller.dart

ImagePicker imagePicker = ImagePicker();
  File? image;

  Future<void> uploadIDPhoto() async {
    final XFile? _image = (await imagePicker.pickImage(
        source: ImageSource.gallery, imageQuality: 50));
    if (image != null) {
      image = File(_image!.path);

      update();
    } else {
      Get.snackbar('Error', 'Please Provide Image');
    }
  }

This is a simple working solution.. in the home_view.dart, just have

        GestureDetector(
          onTap: () async {
            await controller.uploadIDPhoto();
          },
          child: AbsorbPointer(
            child: Input(
              child: TextFormField(
                decoration: InputStyle(label: 'Browse').input(
                  color: AppColor.pink,
                ),
              ),
            ),
          ),
        ),
Hrithik Tiwari
  • 121
  • 1
  • 6
  • 1
    That seems ok, but how can I implement ``` Future retrieveLostData() async { final LostDataResponse response = await picker.retrieveLostData(); if (response.isEmpty) { return; } if (response.file != null) { file.value = response.file; print("second retrived"); } else { retrieveDataError.value = response.exception!.code; } } ``` – Sumit Kumar Nov 26 '21 at 06:33
  • Use of this is stated in the Documentation. – Sumit Kumar Nov 26 '21 at 06:35