1

I am trying to use the image_picker on flutter app everything is fine until gallery open i choose the image then the app will crash and restart.

before this it was working fine like 1 month ago but now it is not. is there any solution

this is my code.

  Future _pickImage() async {
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);

      if (image == null) return;
      final imageTemporary = File(image.path);
      setState(() {
        _image = imageTemporary;
      });
    } catch (error) {
      print("error: $error");
    }
    // setState(() {
    //   _image = img;
    // });
  }
  File? _image;

  Future _pickImage() async {
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);

      if (image == null) return;
      final imageTemporary = File(image.path);
      setState(() {
        _image = imageTemporary;
      });
    } catch (error) {
      print("error: $error");
    }
 
  }
  • show the console response.. the code is fine. immediately it crash is going to give some error in the code.. – Gbenga B Ayannuga Jan 11 '23 at 17:46
  • share crash logs. – Vishal Zaveri Jan 11 '23 at 17:49
  • i got no error at all. i tried on emulator its same. but just now i tried on my friend phone and it's working fine. what could be the reason – Muaadh Abdulmalek Jan 11 '23 at 17:55
  • can you post the image_picker version and you Flutter doctor log? – bakboem Jan 11 '23 at 18:43
  • I was using image_picker: ^0.8.5+3 then i updated to last version this image_picker: ^0.8.6 [√] Flutter (Channel stable, 3.3.10, on Microsoft Windows [Version 10.0.22621.1105], locale en-US) [√] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.3.2) [√] Android Studio (version 2021.2) [√] VS Code (version 1.74.3) [√] Connected device (5 available) [√] HTTP Host Availability • No issues found! – Muaadh Abdulmalek Jan 12 '23 at 06:27
  • I tried on LDPlayer emulator it is working well. As well as my friend phone as i told you. in working on some devices only. it was working fine but i suddenly not working on some devices – Muaadh Abdulmalek Jan 12 '23 at 06:31
  • @MuaadhAbdulmalek were you able to fix it? – Omer M. Aug 22 '23 at 14:48

1 Answers1

0

This code snippet can help you.

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
    


class PickImage extends StatefulWidget {
  const PickImage({Key? key}) : super(key: key);

  @override
  State<PickImage> createState() => _PickImageState();
}

class _PickImageState extends State<PickImage> {
  XFile? _image;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SingleChildScrollView(
        child: Column(
          children: [
            TextButton(
                onPressed: _imageHandler, child: const Text("Pick image")),
            if (_image != null) Image.file(File(_image!.path))
          ],
        ),
      ),
    );
  }

  Future _imageHandler() async {
    try {
      final XFile? imagePicker =
          await ImagePicker().pickImage(source: ImageSource.gallery);
      if (imagePicker != null) {
        _image = imagePicker;
      } else {
        /// user canceled
      }
    } catch (e) {
      print(e);
    }
  }
}