Future pickImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_imageFile = File(pickedFile.path);
});
}
Error is thrown on line File(pickedFile.path), can't compile
The issue was on package, I realized my file had
import 'package:file/file.dart';
package and
import 'dart:io';
I commented out
// import 'package:file/file.dart';
and all is fine now.
Related
I am working on flutter to upload images to the SQL server but there is a error :
type 'Null' is not a subtype of type 'File' of 'function result'
here is how I am initialising the file:
late File _image;
final picker=ImagePicker();
Future choiceImage() async{
var pickedImage= await picker.getImage(source: ImageSource.gallery);
setState(() {
_image=File(pickedImage!.path);
});
}
Future choice2Image() async{
var pickedImage= await picker.getImage(source: ImageSource.camera);
setState(() {
_image=File(pickedImage!.path);
});
}
and getting this image by :
var pic =await http.MultipartFile.fromPath("image", _image.path);
and dont know what is the issue ?
ImagePicker can return a null value if you don't pick any image from the image picker sheet. Therefore, you need to handle null values.
This is what you can do.
File? _image;
final picker=ImagePicker();
Future choiceImage() async{
var pickedImage= await picker.getImage(source: ImageSource.gallery);
if(pickedImage!=null)
{
setState(()
{
_image=File(pickedImage!.path);
});
}
}
Instead of using late File _image;
you can try using:
File? _image;
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);
Error: Unhandled Exception: HiveError: You need to initialize Hive or provide a path to store the box.
Essentially I have these in my dependencies so everything should be good.
hive: ^1.4.4+1
hive_flutter: ^0.3.1
path_provider: ^1.6.27
I also have import 'package:hive/hive.dart';
and
import 'package:path_provider/path_provider.dart'; in the file
So I just have
void doSomething() async {
final documentDirectory = await getApplicationDocumentsDirectory();
Hive.init(documentDirectory.path);
}
called.
I do not understand. I think I've done everything correct. Let me know if you need something else.
Hive needs to be initialized when it runs on Android or iOS, therefore you can use a function like this:
Future<Box> openHiveBox(String boxName) async {
if (!kIsWeb && !Hive.isBoxOpen(boxName))
Hive.init((await getApplicationDocumentsDirectory()).path);
return await Hive.openBox(boxName);
}
You'll need to import path_provider in order to access getApplicationDocumentsDirectory()
Try the following code on the main function of your flutter application:
import 'package:path_provider/path_provider.dart';
import 'package:hive/hive.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocumentDirectory = await getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
}
Currently, path_provider doesn't support WEB. You can see it here: path_provider.
You have to use another directory for WEB. If you are using BLOC as a state management, you could do something like this:
if (!kIsWeb) {
// if android or tablet
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: await getApplicationDocumentsDirectory(),
);
} else {
// if web
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: HydratedStorage.webStorageDirectory,
);
}
I got this error because of a typo:
await Hive.initFlutter;
should've been
await Hive.initFlutter();
I guess you are getting this issue because you are not awaiting the initFlutter.
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
Future<void> yourFunction() async {
final dbDir = await path_provider.getApplicationDocumentsDirectory();
// init hive
await Hive.initFlutter(dbDir.path);
await openYourBox();
}
I think you should await your init method.
Actually you don't need use HydratedStorage to initialize Hive on web:
import 'package:hive/src/hive_impl.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
initializeHive()async{
//Use HiveImpl() to ensure you don't have conflicting Hive boxes.
HiveInterface _hive = HiveImpl();
if (kIsWeb) {
await _hive.openBox('yourBoxName');
} else {
var dir = await getApplicationDocumentsDirectory();
_hive.init(dir.path);
await _hive.openBox('yourBoxName');
}
}
If you're using Flutter on web, you don't need to initialize Hive and neither provider a path to box, only if you're using it on mobile.
I was using the following code to pick images from both Camera and Gallery.
Future<File> getImageFromGallery() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
_image = File(pickedFile.path);
return _image; }
Future<File> getImageFromCamera() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
_image = File(pickedFile.path);
return _image; }
Used Package
import 'package:image_picker/image_picker.dart';
Everything was perfect until I updated flutter to version 1.22.1
After that when I pickup an image the app crashes without any specific error, all I got is
Lost connection to device
Thanks
import 'dart:io';
import 'dart:typed_data';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Imports...
screenshot: ^0.2.0
esys_flutter_share: ^1.0.2
I only need to take a capture, and then share it I am using the following code but I receive the error:
_takeScreenshotandShare() async {
_imageFile = null;
screenshotController
.capture(delay: Duration(milliseconds: 10), pixelRatio: 2.0)
.then((File image) async {
setState(() {
_imageFile = image;
});
final directory = (await getApplicationDocumentsDirectory()).path;
Uint8List pngBytes = _imageFile.readAsBytesSync();
File imgFile = new File('$directory/screenshot.png');
imgFile.writeAsBytes(pngBytes);
print("File Saved to Gallery");
await Share.file('Anupam', 'screenshot.png', pngBytes, 'image/png');
}).catchError((onError) {
print(onError);
});
}
My mistake is:
I/flutter ( 2486): NoSuchMethodError: The method 'findRenderObject' was called on null.
I/flutter ( 2486): Receiver: null
I/flutter ( 2486): Tried calling: findRenderObject()
I face the same issue with screenshot package so my workaround is call same function again incase of error occur.
screenshotController.capture().then((File image) async {
Uint8List pngBytes = image.readAsBytesSync();
final directory = (await getApplicationDocumentsDirectory()).path;
File imgFile = new File('$directory/${DateTime.now().millisecondsSinceEpoch}.png');
await imgFile.writeAsBytes(pngBytes);
if(pngBytes.length == 0)
// call Same function again
else
// your image
}).catchError((onError) {
print(onError);
Future.delayed(Duration(seconds: 1)).then((value) => //call Same function again);
});