Home > Net >  Flutter Error: for the file type in flutter
Flutter Error: for the file type in flutter

Time:05-05

I am using a prebuild flutter project in which below code is giving error at

onImageSelected(croppedImage);

Error

error: The argument type 'CroppedFile?' can't be assigned to the parameter type 'File?'

final Function(File?) onImageSelected;

Future<void> selectedImage(BuildContext context, File? image) async {
    // init i18n
    final i18n = AppLocalizations.of(context);

    // Check file
    if (image != null) {

      final croppedImage = await ImageCropper().cropImage(
          sourcePath: image.path,
          aspectRatioPresets: [CropAspectRatioPreset.square],
          maxWidth: 400,
          maxHeight: 400,
          uiSettings:[ AndroidUiSettings(
            toolbarTitle: i18n.translate("edit_crop_image"),
            toolbarColor: Theme.of(context).primaryColor,
            toolbarWidgetColor: Colors.white,
          )]
    );
      onImageSelected(croppedImage);
    }
  }

How should I rectify it?

CodePudding user response:

A CroppedFile is not a File. You have to create a new File instance from the temporary path given by the image cropper:

if(croppedImage != null) {
    onImageSelected(File(croppedImage.path));
}

CodePudding user response:

The ImageCropper returns the type Future<CroppedFile?>. So what you have is a variable of type CroppedFile.

The callback onImageSelected seems to take a variable of type File? instead.

By looking at the code, it seems CroppedFile is a limited subset of File

https://github.com/hnvn/flutter_image_cropper/blob/master/image_cropper_platform_interface/lib/src/models/cropped_file/base.dart

What might work is to declare the variable of croppedImage with the type File? like:

File? croppedFile = await ImageCropper().cropImage(...);

This is also what they show in their examples:

import 'package:image_cropper/image_cropper.dart';

File? croppedFile = await ImageCropper().cropImage(
      sourcePath: imageFile.path,
      aspectRatioPresets: [
        CropAspectRatioPreset.square,
        CropAspectRatioPreset.ratio3x2,
        CropAspectRatioPreset.original,
        CropAspectRatioPreset.ratio4x3,
        CropAspectRatioPreset.ratio16x9
      ],
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: 'Cropper',
            toolbarColor: Colors.deepOrange,
            toolbarWidgetColor: Colors.white,
            initAspectRatio: CropAspectRatioPreset.original,
            lockAspectRatio: false),
        IOSUiSettings(
          title: 'Cropper',
        ),
        /// this settings is required for Web
        WebUiSettings(
          context: context,
          presentStyle: CropperPresentStyle.dialog,
          boundary: Boundary(
            width: 520,
            height: 520,
          ),
          viewPort: ViewPort(
            width: 480,
            height: 480,
            type: 'circle'
          ),
          enableExif: true,
          enableZoom: true,
          showZoomer: true,
        )
      ],
    );
  • Related