Reputation: 179
I recentely added the plugin image_picker: ^0.8.5+3 from flutter pub dev. I integrated it to my flutter codes. here is the code.
This code is supposed to capture image then the image will be used at the same page in the imgRabbitdflt1,
import 'dart:io';
import 'controller/scan_controller.dart';
import 'package:flutter/material.dart';
import 'package:grabbitapp/core/app_export.dart';
import 'package:image_picker/image_picker.dart';
class ScanScreen extends GetWidget<ScanController> {
File? image;
Future pickimage() async {
final image = await ImagePicker().pickImage(source: ImageSource.camera);
if (image == null) return;
final imageTemporary = File(image.path);
setState (() => this.image = imageTemporary);
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: ColorConstant.whiteA700,
body: Container(
width: size.width,
child: SingleChildScrollView(
child: Container(
decoration:
BoxDecoration(color: ColorConstant.whiteA700),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: double.infinity,
margin: EdgeInsets.only(
left: getHorizontalSize(10.00),
top: getVerticalSize(135.00),
right: getHorizontalSize(10.00)),
decoration: BoxDecoration(
color: ColorConstant.bluegray100,
borderRadius: BorderRadius.circular(
getHorizontalSize(15.00))),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: EdgeInsets.only(
left: getHorizontalSize(5.00),
top: getVerticalSize(50.00)),
child: image != null ? Image.file(image!) : Image.asset(
ImageConstant.imgRabbitdflt1,
height: getVerticalSize(236.00),
width:
getHorizontalSize(320.00),
fit: BoxFit.fill)),
GestureDetector(
onTap: () {
onTapImgCameraicon();
},
child: Padding(
padding: EdgeInsets.only(
left: getHorizontalSize(
10.00),
top: getVerticalSize(43.00),
right: getHorizontalSize(
10.00),
bottom:
getVerticalSize(27.29)),
child: Image.asset(
ImageConstant.imgCameraicon,
height:
getVerticalSize(77.71),
width: getHorizontalSize(
82.93),
fit: BoxFit.fill)))
])),
Padding(
padding: EdgeInsets.only(
left: getHorizontalSize(10.00),
top: getVerticalSize(135.10),
right: getHorizontalSize(10.00),
bottom: getVerticalSize(20.00)),
child: GestureDetector(
onTap: () {
onTapBtnNext();
},
child: Container(
alignment: Alignment.center,
height: getVerticalSize(40.60),
width: getHorizontalSize(267.48),
decoration: AppDecoration
.textstylemontserratromanmedium20,
child: Text("lbl_next".tr,
textAlign: TextAlign.center,
style: AppStyle
.textstylemontserratromanmedium20
.copyWith(
fontSize: getFontSize(20),
letterSpacing: 1.20)))))
]))))));
}
onTapImgCameraicon() async {
await PermissionManager.askForPermission(Permission.camera);
await PermissionManager.askForPermission(Permission.storage);
List<String?>? imageList = [];
//TODO: Permission - use imageList for using selected images
await FileManager().showModelSheetForImage(getImages: (value) async {
imageList = value;
});
}
onTapBtnNext() {
Get.toNamed(AppRoutes.rabbitGeneratedInfoScreen);
}
void setState(File Function() param0) {}
}
But when I tried to run it, I got these errors,
/E:/src/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker_android-0.8.5+1/lib/image_picker_android.dart:174:5: Error: Type 'ImagePickerOptions' not found.
ImagePickerOptions options = const ImagePickerOptions(),
^^^^^^^^^^^^^^^^^^
/E:/src/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker_android-0.8.5+1/lib/image_picker_android.dart:174:40: Error: Couldn't find constructor 'ImagePickerOptions'.
ImagePickerOptions options = const ImagePickerOptions(),
^^^^^^^^^^^^^^^^^^
/E:/src/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker_android-0.8.5+1/lib/image_picker_android.dart:174:5: Error: 'ImagePickerOptions' isn't a type.
ImagePickerOptions options = const ImagePickerOptions(),
^^^^^^^^^^^^^^^^^^
DId I miss to import something or what are nmissing that led to these errors?
Upvotes: 3
Views: 5931
Reputation: 324
I also had the same problem with image_picker ^0.8.5+3, such as that missing type 'ImagePickerOptions'.
Next thing I did was to go into their github repo and found out that this type was added +- recently into its sub-dependency project image_picker_platform_interface
, but in a more recent version of that sub-project (current latest is image_picker_platform_interface 2.6.1 and it does contain the new type that we're looking for...).
Next, I went and checked on my local what version of that sub-dependency was being pulled in when installing image_picker ^0.8.5+3
, by doing the command "ls -l ~/.pub-cache/hosted/pub.dartlang.org/ | grep image_picker_platform_interface
" on my local terminal / command line, to realize that I was running on an older image_picker_platform_interface ^2.4.4
, which does not contain the new type that we need for image_picker ^0.8.5+3
.
(btw, here's image_picker pubspec.yaml showing that it's trying to pull the sub-dependency image_picker_platform_interface ^2.3.0
, e.g. anything higher or equal to 2.3.0 basically -> that is the real problem with the lib actually that will hopefully get fixed later in newer versions, the publisher of this lib should have increased that sub-dependency 'minimum version' to force all of us to get the newer sub-dependency downloaded or else our system will accept any older version that respects ^2.3.0, so 2.4.4 in my case which is missing classes/types required by image_picker 0.8.5+3...)
At this point, you have 2 options to solve this issue:
1- Either you go remove that specific older version of image_picker_platform_interface
from your pubspec.lock
like others similarly mentioned, e.g. you don't have to delete the entire .lock file I actually don't recommend it (or simply replace its version from whatever older version you have with 2.6.1)
2- Or you could force momentarily in your flutter project's pubspec.yaml
a specific version of that image_picker_platform_interface sub-dependency, by adding "image_picker_platform_interface: ^2.6.1
" to the "dependency_overrides
" section (if you don't have that section already, just add it as a new section perhaps under your "dependencies" section), that will also force your flutter app to download that version. But once the right 2.6.1 gets later downloaded, you should comeback to your pubspec.yaml and delete that unnecessary override, to not be stuck with it forever, as it is not usually recommended to have those overrides, here it is only used temporary to help us fix the sub-dependency issue.
At this point, before getting the new sub-dependency 2.6.1 downloaded, I would recommend that you simply delete the older version you may have in your local pub cache (e.g., in my case I had the 2.4.4. so the command was "rm -rf ~/.pub-cache/hosted/pub.dartlang.org/image_picker_platform_interface-2.4.4
").
And finally, you can either simply start your flutter app ("flutter run
") or issue the command "flutter pub get
" to get that sub-dependency 2.6.1 version downloaded, and then verify that it's well present on your local flutter dependencies pub cache by issuing the same 'ls
' command as mentioned previously.
Hope this helps you or anyone else. Cheers
Upvotes: 2
Reputation: 91
I deleted the pubspec.lock
, and ran the command flutter pub get
.
Upvotes: 9
Reputation: 120
Remove the package from dependencies in pubspec.yaml, run
flutter packages get
. And then add the package to dependencies again and running
flutter packages get
.This process has solved the problem for me in the past.
Upvotes: 1
Reputation: 393
please clean your project and after run flutter pub get it will work and if still it is not working try to clear cache of your project it will work.
Upvotes: 4