skip to Main Content

I got a problem in imagepicker which is pick image is red underlined which mean undefined class.
enter image description here

anyone can give the solution?

here is my code:

class _homepageState extends State<homepage> {
String? _path = null;

void _showPhotoLibrary() async {
 final file = await ImagePicker.pickImage(source: ImageSource.gallery);

 setState(() {
   _path = file!.path;
 });

 void _showCamera() async {
   final cameras = await availableCameras();
   final camera = cameras.first;

   final result = await Navigator.push(
       context,
       MaterialPageRoute(
           builder: (context) => TakePicturePage(camera: camera)));

   setState(() {
     _path = result;
   });
 }

2

Answers


  1. Try to initialize ImagePicker like this

    final ImagePicker _picker = ImagePicker();
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    

    Then you can access pickImage

    Login or Signup to reply.
  2. I Used This code for image_picker its working fine for me please try this way hope this works for you
    
    ///first you have to create this line 
    File ?_image;
      final picker = ImagePicker();
    
    ////alertbox to choose image from gallery or camera
     Future<void>_showChoiceDialog(BuildContext context)
      {
        return showDialog(context: context,builder: (BuildContext context){
    
          return AlertDialog(
            title: Text("Choose option",style: TextStyle(color: AppColors.hotelListDarkBlue),),
            content: SingleChildScrollView(
              child: ListBody(
                children: [
                  Divider(height: 1,color: AppColors.baseLightBlueColor,),
                  ListTile(
                    onTap: (){
                      _getImage(ImageSource.gallery);
                    },
                    title: Text("Gallery",style: TextStyle(color: AppColors.hotelListDarkBlue),),
                    leading: Icon(Icons.account_box,color: AppColors.baseLightBlueColor,),
                  ),
    
                  Divider(height: 1,color: AppColors.baseLightBlueColor,),
                  ListTile(
                    onTap: (){
                      _getImage(ImageSource.camera,);
                    },
                    title: Text("Camera",style: TextStyle(color: AppColors.hotelListDarkBlue),),
                    leading: Icon(Icons.camera,color: AppColors.baseLightBlueColor,),
                  ),
                ],
              ),
            ),);
        });
      }
    
    ////to show image from image picker 
     Container(
                          height: 150,
                          width:_width,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                height: 120,
                                width: 120,
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  shape: BoxShape.circle,
                                  image: DecorationImage(
                                    fit: BoxFit.fill,
                                    image:  FileImage(_image!),
                                  ),
                                  border: Border.all(color: AppColors.white, width: 2.0),
                                ),
                              ),
                            ],
                          ),
                        ),
    
    ////cretae a method to get image
     _getImage(ImageSource imageSource) async
      {
        PickedFile? imageFile = await picker.getImage(source: imageSource);
    //if user doesn't take any image, just return.
        if (imageFile == null) return;
        setState(
              () {
    //Rebuild UI with the selected image.
            _image = File(imageFile.path);
          },
        );
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search