'flutter image picker not working and crashing app without error on debug

I'm currently using these versions: flutter : 2.16.0 image_picker : ^0.8.4+7

The image picker is not working. After running the app, when I click on the button to activate the pickImage function, running suddenly stops and the app crashes and stops. On debug, the only message I get is:

Lost connection to device.

Here's the code:

import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hipocampus_editors/widgets/textformfield_widget.dart';
import 'package:image_picker/image_picker.dart';

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

  @override
  _AddSystemPageState createState() => _AddSystemPageState();
}

class _AddSystemPageState extends State<AddSystemPage> {
  final _formKey = GlobalKey<FormState>();
File? image1;

  Future pickImage() async{
    
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null)  return;
final imageTemporary = File(image.path);
setState(() {
  image1 = imageTemporary;
});

    } 
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        appBar: AppBar(title: const Text('System',),),
        
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 5),
            child: Form(
              key: _formKey,
              child: Container(
                width: MediaQuery.of(context).size.width,
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: SingleChildScrollView(
                    child: Column(
                  children: [
                    
                    ElevatedButton(onPressed: (){
                      pickImage();
                                          }, child: Text('Select image'))
                    
                  ],
                )),
              ),
            ),
          ),
        ),
      ),
    );
    }
    }


Solution 1:[1]

Add this to iOS>runner>Info.plist

<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>

Solution 2:[2]

Please check if you have registered the ImagePicker Correctly.

and if you have registered ImagePicker correctly then put a try and catch blog in the pick Image Function to get more debug information:

Future pickImage() async{
    
  try{
    final image = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (image == null)  return;
    final imageTemporary = File(image.path);
    setState(() {
      image1 = imageTemporary;
    });
  } catch(error) {
    print("error: $error");
  }

} 

Solution 3:[3]

This happened when I was trying with iOS simulator. You need to add these keys inĀ /ios/Runner/Info.plist:

NSPhotoLibraryUsageDescription NSCameraUsageDescription

Also try to test your app in physical device. And if you working for Android version then no configuration changes are required and you can refer to this video: https://youtu.be/s0YqbEJcRtE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mateus Neves
Solution 2 liam spiegel
Solution 3 Akanksha Singh