Flutter Error: for the file type in flutter

Flutter Error: for the file type in flutter

我正在使用预构建的 flutter 项目,其中以下代码在

处出现错误

onImageSelected(croppedImage);

错误

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

我该如何纠正?

CroppedFile 不是文件。您必须根据图像裁剪器提供的临时路径创建一个新的文件实例:

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

ImageCropperreturns类型Future<CroppedFile?>。所以你有一个 CroppedFile.

类型的变量

回调 onImageSelected 似乎采用 File? 类型的变量。

通过查看代码,似乎 CroppedFileFile

的有限子集

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

可能有用的方法是声明 croppedImage 类型为 File? 的变量,例如:

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

这也是他们在示例中展示的内容:

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,
        )
      ],
    );