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

Viewed 4312

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'))
                    
                  ],
                )),
              ),
            ),
          ),
        ),
      ),
    );
    }
    }
4 Answers

Add this to iOS>runner>Info.plist

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

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");
  }

} 

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

For developing on apple silicon (M1), I needed to remove the google_maps plugin. This plugin moves the simulator to Rosetta and this has some issues with image handling in the simulator.

Related