Home > Back-end >  Flutter, image_picker showing nothing when pressed
Flutter, image_picker showing nothing when pressed

Time:12-19

image_picker looks fine when i use it in emulator, when it's pressed it works properly but when i install it to my phone it does nothing when i pressed the button for image_picker no camera show just nothing change.

i think it has to be some persmission" things but i don't quite understand with it

method:

var selectedImagePath = ''.obs;

getImage(ImageSource ImageSource) async {
  final pickedFile = await ImagePicker().pickImage(source: ImageSource);
  if (pickedFile != null) {
    selectedImagePath.value = pickedFile.path;
  } else {
    Get.snackbar('Error', 'No Image Selected');
  }
}

display:

Scaffold(
  body: SafeArea(
    child: Column(
      children: [
        Obx(
          () => Container(
            width: double.infinity,
            height: 400,
            decoration: selectedImagePath == ''
                ? BoxDecoration(
                    color: Colors.grey,
                  )
                : BoxDecoration(
                    color: Colors.grey,
                    image: DecorationImage(
                      image: FileImage(
                        File(
                          selectedImagePath.value,
                        ),
                      ),
                      fit: BoxFit.cover
                    ),
                  ),
          ),
        ),
        SizedBox(height: 10),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ElevatedButton(
              onPressed: () {
                getImage(ImageSource.camera);
              },
              child: Text('Take a photo'),
            ),
            ElevatedButton(
              onPressed: () {
                getImage(ImageSource.gallery);
              },
              child: Text('Take from gallery'),
            ),
          ],
        ),
      ],
    ),
  ),
);

CodePudding user response:

This works for me

File? _imageFile;
Future<void> getImage(
      ImageSource source) async {
    XFile? pickedFile = await ImagePicker()
        .pickImage(source: source);
    if (pickedFile != null) {
      File selected = File(pickedFile.path);
      _imageFile = selected;
    }
  }

Let me know if it works for you.

CodePudding user response:

Here is my code, I'm using Provider Library to manage it.

Step 1:

//File? _image;
String? imgString;
ImagePicker? imagePicker = ImagePicker();

Step 2: Widget Code

Future getImage() async {
final dynamic image =
    await imagePicker?.pickImage(source: ImageSource.gallery);
setState(() {
  if (image != null) {
    YourProvider.myImage = File(image.path);
    YourProvider.pickedImage = true;
    imgString = YourBaseUrl   YourProvider.myImage!.path;
    debugPrint("Image: $imgString");
  }
});

}

Step 3: Provider

  bool pickedImage = false;
  File? myImage;
  • Related