Flutter 程序包androidx.lifecycle不存在 错误解决

本文介绍如何在Flutter中实现头像选择、裁剪及上传功能。通过使用image_picker和image_cropper插件,可以轻松地从相机或相册选择图片,并进行裁剪调整。文章还解决了Androidx兼容性问题,确保应用正常运行。

flutter 选择图片并裁剪上传头像功能

1、引用 选择图片库

image_picker: ^0.6.3+1

//选择照片

Utils.showChoose(context, "选择照片来源", photoArray, (int position) async {
  var image = await ImagePicker.pickImage(source: position==0 ? ImageSource.camera : ImageSource.gallery);

2、引用图片裁剪库

image_cropper: ^1.2.1

//裁剪照片

File croppedFile = await ImageCropper.cropImage(
    sourcePath: image.path,
    maxWidth: 300,
    maxHeight: 300,
    aspectRatioPresets: [
      CropAspectRatioPreset.square
    ],
    androidUiSettings: AndroidUiSettings(
        toolbarTitle: '裁剪',
        toolbarColor: Colors.white,
        toolbarWidgetColor: MyColors.blue,
        initAspectRatio: CropAspectRatioPreset.square,
        lockAspectRatio: true),
    iosUiSettings: IOSUiSettings(
      minimumAspectRatio: 1.0,
    )
);

获取到裁剪后的照片后调用上传接口即可。

编译运行报错 程序包androidx.lifecycle不存在。

这个问题是Androidx引用库兼容问题。找到<project_root>/android/app/build.gradle 在最后增加如下代码即可

configurations.all {
    resolutionStrategy {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'androidx.core') {
                details.useVersion "1.0.2"
            }
            if (details.requested.group == 'androidx.lifecycle') {
                details.useVersion "2.0.0"
            }
            if (details.requested.group == 'androidx.versionedparcelable') {
                details.useVersion "1.0.0"
            }
            if (details.requested.group == 'androidx.fragment') {
                details.useVersion "1.0.0"
            }
            if (details.requested.group == 'androidx.appcompat') {
                details.useVersion "1.0.1"
            }
        }
    }
}

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值