0

I have two image buttons in flutter application to pick images from the gallery, when I press anyone ImageButton and try to select the image it gets picked successfully. and after that when I try to use other imagebutton to pick another image the app gets crashed in iOS

To pick the image everytime the different calls are done for getting the image.

Below are the methods called everytime

Future getImage2() async {
    print("get image");

    PickedFile image = await _picker2.getImage(source: ImageSource.gallery);

   // File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      setState(() {
        final File file = File(image.path);
        avatarImageFile2 = file;
        isLoading2 = true;
        

      });
    }
  }


Future getImage1() async {
    print("get image");

    PickedFile image = await _picker1.getImage(source: ImageSource.gallery);

   // File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      setState(() {
        final File file = File(image.path);
        avatarImageFile1 = file;
        isLoading1 = true;
       

      });
    }
  }

please guide how should I resolve it

1 Answer 1

0

If you are not added permission, then Add the following keys to your Info.plist file, located in /ios/Runner/Info.plist:

NSPhotoLibraryUsageDescription - describe why your app needs permission for the photo library. This is called Privacy - Photo Library Usage Description in the visual editor.

NSCameraUsageDescription - describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.

NSMicrophoneUsageDescription - describe why your app needs access to the microphone, if you intend to record videos. This is called Privacy - Microphone Usage Description in the visual editor.

If you are already added this Try this code.

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

 void main() => runApp(MyApp()); 



class MyApp extends StatelessWidget { 
@override 
Widget build(BuildContext context) {
 return MaterialApp( home: MyHomePage(), 
    ); 
   }
 }


 class MyHomePage extends StatefulWidget { @override
 _MyHomePageState createState() =>
_MyHomePageState();
 } 


class _MyHomePageState extends State<MyHomePage> {

 File _image; final picker = ImagePicker(); 

Future getImage() async { 
final pickedFile = await picker.getImage(source: ImageSource.camera);

 setState(() {
 if (pickedFile != null) {
 _image = File(pickedFile.path); 
} else {

 print('No image selected.');
 }
 }); 
} @override
 Widget build(BuildContext context) { 

return Scaffold(
 appBar: AppBar( 

title: Text('Image Picker Example'),

 ),

 body: Center( child: _image == null ? Text('No image selected.') : Image.file(_image), 

), 

floatingActionButton: FloatingActionButton( onPressed: getImage,

 tooltip: 'Pick Image',
 child: Icon(Icons.add_a_photo),
 
        ),
      ); 
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.