Im getting a lot of different errors when I try to pick the image from my gallery and it does not show the image I picked when it should. Please let me know where I'm going wrong. Here is my code
File? image;
Future getImage() async {
var img = await ImagePicker.platform.pickImage(source: ImageSource.gallery);
setState(() {
image = img as File;
});
}
InkWell(
child: CircleAvatar(
backgroundImage: image != null ? FileImage(image!) : NetworkImage("null") as ImageProvider,
radius: 100,
),
onTap: () async {
getImage();
}
These are my errors:
The following ArgumentError was thrown resolving an image codec:
Invalid argument(s): No host specified in URI file:///null
E/flutter (10373): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'PickedFile' is not a subtype of type 'File' in type cast E/flutter (10373): #0 _AssetListDetailsAddState.getImage. (package:asset_management/main.dart:156:19) E/flutter (10373): #1 State.setState (package:flutter/src/widgets/framework.dart:1121:30) E/flutter (10373): #2 _AssetListDetailsAddState.getImage (package:asset_management/main.dart:155:5) E/flutter (10373): E/flutter (10373):
CodePudding user response:
The exception looks pretty clear:
Invalid argument(s): No host specified in URI file:///null
This is breaking your code: NetworkImage("null") as ImageProvider
, you're passing a string "null".
I'd suggest either to use a "Default" image you store locally or you fetch from a link, or to go with:
backgroundImage: image != null ? FileImage(image!) : null,
radius: 100,
),
CodePudding user response:
Try fix getImage()
to this:
Future getImage() async {
PickedFile? img = await ImagePicker.platform.pickImage(source: ImageSource.gallery);
setState(() {
image = File(img.path ?? "");
});
}
Widget InkWell:
InkWell(
child: CircleAvatar(
backgroundImage: image != null
? FileImage(image!)
: NetworkImage(
"https://jes.edu.vn/wp-content/uploads/2017/10/hình-ảnh.jpg")
as ImageProvider,
radius: 100,
),
onTap: () async {
getImage();
},
)