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

Time:02-10

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

CodePudding user response:

Add this to iOS>runner>Info.plist

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

CodePudding user response:

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

} 
  • Related