alex
alex

Reputation: 17

I have Issues with image picker

I created a method to pick images in the authcontroller class like so

class AuthController{

  pickImage(ImageSource source)async{
    final ImagePicker _picker = ImagePicker();
      XFile? _file = await _picker.pickImage(source: source);
      if(_file != null){
        await _file.readAsBytes();
      }else{
        print('no image selected');
      }

  }
}

In my main code I created two functions to specify from where exactly we should pick the images after creating a global variable called _image

 Uint8List? _image;
 selectGalleryImage()async{
    Uint8List? _im=await _authController.pickImage(ImageSource.gallery);
    setState(() {
      _image = _im;
    });
  }
  selectCameraImage()async{
    Uint8List? _im=await _authController.pickImage(ImageSource.camera);
    setState(() {
      _image = _im;
    });
  }

the problem is when i try to call display _image from either my camera or gallery, it does not display. I can pick images from both mediums, it is just that the image does not display even though i pass it through MemoryImage in the CircleAvater Widget

 _image != null ? CircleAvatar(
                            radius: 44,
                            backgroundImage: MemoryImage(_image!,)
                          ):const CircleAvatar(
                            radius: 44,
                            backgroundColor: Colors.black,
                            backgroundImage: NetworkImage('https://t4.ftcdn.net/jpg/00/64/67/63/360_F_64676383_LdbmhiNM6Ypzb3FM4PPuFP9rHe7ri8Ju.jpg'),
                          ),

I have added everything addable in the androidmanifest in the main folder.

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <application

and i have run flitter clean multiple times but it still give me the same result. help would be very much appreciated

Upvotes: 1

Views: 236

Answers (1)

rrttrr
rrttrr

Reputation: 1856

As one comment says, pickImage is not returning anything.

Change it to this,

class AuthController{
  Future<Uint8List?> pickImage(ImageSource source)async{
    final ImagePicker _picker = ImagePicker();
    XFile? _file = await _picker.pickImage(source: source);
    if(_file != null){
      return await _file.readAsBytes();
    }else{
      print('no image selected');
      return null;
    }
  }
}

Upvotes: 1

Related Questions