Setting image as default using Uint8List and ImagePicker - flutter

I have a profile screen that prompts user to enter information and there's a image picker for the profile image.
I use Uint8List to store the picked image and upload to a database.
How do I make an image in assets the default image if the user doesn't select an image?
class _SignUpScreenState extends State<SignUpScreen> {
Uint8List? _image;
#override
void dispose() {...}
void selectImage() async {
Uint8List imageFile = await pickImage(ImageSource.gallery);
setState(() {
_image = imageFile;
});
}
void signupUser() async {
final navigator = Navigator.of(context);
if (_image == null) {
//set default image as profile
showSnackBar('Please select image', context);
return;
}
setState(() {...}
My ImagePicker is as follows:
pickImage(ImageSource source) async {
final ImagePicker _imagePicker = ImagePicker();
XFile? _file = await _imagePicker.pickImage(source: source);
if (_file != null) {
return await _file.readAsBytes();
}
}
showSnackBar(String content, BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(content),
),
);
}
The default image is an SVG as well but I have a png too.

Simply check the null state of Unit8List variable
/// for png assets
_image == null ? Image.asset("<asset file path>") : Image.memory(_image!)
For svg image use flutter_svg package
_image == null ? SvgPicture.asset("<asset file path>") : Image.memory(_image!)

Related

Flutter - The argument type 'File' can't be assigned to the parameter type 'File?'

I am building a Flutter app and currently I am trying to implement a method that can pick an image file but I am getting an error that says The argument type 'File' can't be assigned to the parameter type 'File?'. Does anyone have an idea on how I can solve this?
getImageDialogue(ImageSource source) async {
final ImagePicker _pickedFile = ImagePicker();
final XFile? image = await _pickedFile.pickImage(source: source);
if (image != null) {
media.add(EventMediaModel(
//This is the line where I am getting the error on File(image.path)
image: File(image.path), video: null, isVideo: false));
setState(() {});
Navigator.pop(context);
}
}
It might help to create a non-nullable "image" variable in your EventMediaModel.
Future<void> getImageDialogue(ImageSource source) async {
final ImagePicker _pickedFile = ImagePicker();
final XFile? image = await _pickedFile.pickImage(source: source);
if (image != null && image.path != '') {
File file = File(image.path);
media.add(EventMediaModel(image: file, video: null, isVideo: false));
setState(() {});
Navigator.pop(context);
}
}

Flutter Newb, anyone tell me whats wrong with this code?

Following a tutorial for adding and saving images and getting errors for the below code, under ImagePicker, ImageSource and SelectedImage.
Future getImage() async {
var image = await ImagePicker.PickImage(source: ImageSource.camera);
setState(() {
selectedimage = image;
});
}
You need to create an instance first, then you will be able to use pickImage method.
Future getImage() async {
XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
if (image != null) {
setState(() {
selectedimage = image;
});
}
}
Fine more about image_picker
You have to be careful whenever playing with the setState() functions. This combined with how you pick the image and how you store it in the 'var' in your case could cause further trouble. To generally ease things up for further use I recommend creating a utils class, in which you would have an image picker method, just like this one:
import 'package:image_picker/image_picker.dart';
pickImage(ImageSource source) async {
final ImagePicker _imagePicker = ImagePicker();
XFile? _file = await _imagePicker.pickImage(source: source);
if (_file != null) {
return await _file.readAsBytes();
}
print('No Image Selected');
}
Afterwards, if you would like to call that in any other instance you would need something like this, though this is for a url image(I had it in hand), such as:
void selectImage() async {
Uint8List? im = await pickImage(ImageSource.gallery);
final ByteData imageData = await NetworkAssetBundle(Uri.parse(
"url for template image"))
.load("");
final Uint8List bytes = imageData.buffer.asUint8List();
// if null - use the template image
im ??= bytes;
// update state
setState(() {
_image = im!;
});
}
Hope it helped in a way.

A value of type 'XFIle' can't be assigned to a variable of type 'File' error

I am using image_picker: ^0.8.4+4 where I am getting this error. what I can do to make this code right?
late File selectedImage;
bool _isLoading = false;
CrudMethods crudMethods = CrudMethods();
Future getImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
selectedImage = image; //A value of type 'XFIle' can't be assigned to a variable of type 'File' error.
});
}
uploadBlog() async {
// ignore: unnecessary_null_comparison
if (selectedImage != null) {
setState(() {
_isLoading = true;
});
you can conver XFile to File using following line:
selectedImage = File(image.path);
First, you should create your variable as XFile
Because this is what you get from image picker.
XFile photo;
void _pickImage() async {
final ImagePicker _picker = ImagePicker();
photo = await _picker.pickImage(source: ImageSource.camera);
if (photo == null) return;
}
And then you can use your image as a file image.
Image.file(File(photo.path))
This happens because the package (image_picker ) you are using is relying on XFile and not File, as previously did.
So, first you have to create a variable of type File so you can work with later as you did, and after fetching the selectedImage you pass the path to instantiate the File. Like this:
File? selectedImage;
bool _isLoading = false;
CrudMethods crudMethods = CrudMethods();
Future getImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
selectedImage = File(image!.path); // won't have any error now
});
}
//implement the upload code
XFile and File can be converted to each other like the following in Flutter:
import 'dart:io';
import 'package:camera/camera.dart';
XFile takenPhotoXFile = await _controller!.takePicture();
// XFile to File
File photoAsFile = File(takenPhotoXFile.path);
// File to XFile
XFile imageFileAsXFile = XFile(photoAsFile.path);

Convert Uint8List to File

I'm using Image Picker web which works well. I can display image in Image.memory(), but this image in format Uintlist8. For save in storage need format File, my issue is how to save an image in Firebase Storage.
Web image picker:
class _SecondPageState extends State<SecondPage> {
final _formkey = GlobalKey<FormState>();
Uint8List _image;
getImage() async {
Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);
if (tempImg != null) {
setState(() {
_image = tempImg;
});
}
}
Please Try ....
final _formkey = GlobalKey<FormState>();
Uint8List _image;
getImage() async {
Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);
if (tempImg != null) {
setState(() {
_image = tempImg;
final tempDir = await getTemporaryDirectory();
final file = await new File('${tempDir.path}/image.jpg').create();
file.writeAsBytesSync(_image);
});
}
}

Converting of Image File created using image_picker package of flutter to AssetImage/Image.asset?

I am new to flutter, i am building an app where i need to convert the file(image) generated after using image_picker package to asset image to use in the app.
example code as follows, which creates file(Image)
final Function onSelectImage;
ImageInput(this.onSelectImage);
File _storedImage;
Future<void> _takePicture() async {
final imageFile = await ImagePicker.pickImage(
source: ImageSource.camera,
maxWidth: 600,
);
if (imageFile == null) {
return;
}
setState(() {
_storedImage = imageFile;
});
final appDir = await syspaths.getApplicationDocumentsDirectory();
final fileName = path.basename(imageFile.path);
final savedImage = await imageFile.copy('${appDir.path}/$fileName');
widget.onSelectImage(savedImage);
}
Thanks in advance
You can create an image variable which you can rever to and update when you selected the image.
See the following code:
final Function onSelectImage;
ImageInput(this.onSelectImage);
File _storedImage;
Image _tempImage;
Future<void> _takePicture() async {
final imageFile = await ImagePicker.pickImage(
source: ImageSource.camera,
maxWidth: 600,
);
if (imageFile == null) {
return;
}
setState(() {
_storedImage = imageFile;
});
final appDir = await syspaths.getApplicationDocumentsDirectory();
final fileName = path.basename(imageFile.path);
final savedImage = await imageFile.copy('${appDir.path}/$fileName');
widget.onSelectImage(savedImage);
setState(() {
_tempImage = imageFile;
});
}
#override
Widget build(BuildContext context) {
return _tempImage == null ? Container(child:null) : Image(image: _tempImage);
}