Home > database >  Flutter Upload Images
Flutter Upload Images

Time:01-03

enter image description here

enter image description here

I try to use upload image from flutter libery, 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

CodePudding user response:

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

CodePudding user response:

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;
  }
  • Related