Flutter Upload Images

I try to use upload image from Flutter libary, but it doesn't work. The codes just run to gallery and choose the image, but the image doesn't show to the screen.

Please help me to make it work, from choosing the image and show the image result that I choose. Thanks

Image 1

Image 2

Upvotes: 0

Views: 674

Answers (2)

Mukund Jogi
Mukund Jogi

Reputation: 1395

I hope this answer will help you.

static String? pickImage({bool useCamera = false, bool crop = true,
      CropAspectRatio ratio = const CropAspectRatio(
        ratioX: 1, ratioY: 1,
      )}) {
    ImagePicker().pickImage(source: useCamera ? ImageSource.camera : ImageSource.gallery, imageQuality: 60).then((pickedFile) {
      if (pickedFile == null || !File(pickedFile.path).existsSync()) {
        return Stream.error(Exception('No image picked!'));
      }
      if (crop) {
        ImageCropper().cropImage(sourcePath: pickedFile.path ?? '', aspectRatio: ratio, uiSettings: [
          AndroidUiSettings(
              toolbarTitle: 'Cropper',
              toolbarColor: AppStyles.primary500Color,
              toolbarWidgetColor: Colors.white,
              initAspectRatio: CropAspectRatioPreset.original,
              lockAspectRatio: false),
          IOSUiSettings(
            title: 'Cropper',
          ),
        ]).then((value) {
          if (value != null) {
            croppedFile = value;
            // selectedPhotoStream.add(croppedFile);
            /// ** You are missing **
            setState((){
              uploadFile = croppedFile.path;
            });  
            return croppedFile?.path;
          }
        });
      }
    });
    return croppedFile?.path;
  }

Upvotes: 0

Snehal Singh
Snehal Singh

Reputation: 116

You have to update the state of the screen for seeing the changes. If you are using stateful just simply use setState to do this.

setState(() {
  uploadFile = File(image!.path);
});

Upvotes: 2

Related Questions