How to add File to Array (List)
I got an image from ImagePicker
var image = await ImagePicker.pickImage(source: imageSource);
_images are declared by followed
List<File> _images = List<File>();
I tried as Follows
_images = image as List<File>
But I got errors like this
Unhandled Exception: type '_File' is not a subtype of type
'List' in type cast
You're getting the error because Future<File> pickImage(..) returns File not List<File>.
In you code you're trying to cast File as List<File>:
_images = image as List<File>
If you only want to store the File returned from Future<File>pickImage(..), then you can add the File to the List
_images.add(image);
Also Future<File>pickedImage(..) method is deprecated so please try using Future<PickedFile> getImage(..), which returns PickedFile instead of File.
Related
[here is the issue (https://i.stack.imgur.com/D0zPF.png)
I have tried to run flutter clean and flutter pub get but no changes I don't know what causing this issue.
I will appreciate your help guys.
Could you try this please:
chooseFile() async {
final chosenFile = await ImagePicker().pickImage(source: ImageSource.gallery);
if(chosenFile != null) {
return chosenFile.path;
}
}
If it does not work could you please show me the code you've tried before and explain the desired results.
When I build after changing Xfile to file, it says null check operator user on a null value, but I don't know what the problem is
The code is too long, so I'll just write the gist
please help me......
XFile? _imageFile;
File file = File(_imageFile!.path);
Are you picking any file in XFile or just declaring it?
Then of course you're going to get null value
Otherwise,
Add a check when you've picked a file from user
XFile? imageFile;
imageFile = //Your File Picking logic
if(imageFile != null) {
File _file = File(imageFile!.path);
}
Else you can also try reading the bytes if in any case your file path is inaccessible and create a new file object
imageFile!.readAsBytes();
What does a null check operator?
This operator (!) throws an exception when a nullable variable wasn't initialized (so, it's null).
How do I fix the error?
Since in your code the XFile? _imageFile variable is nullable (indicated by the ?), probably it turns out to be actually null when being assigned to a File object.
A quick solution the null check problem could be this:
late File file;
XFile? _imageFile;
if (_imageFile != null) {
file = File(_imageFile!.path);
}
But in your exact code, what you should really do is initialize _imageFile, as right now you are just declaring it.
XFile? _imageFile = await ImagePicker().pickImage(source: ImageSource.gallery); //Example with image_picker package
i'm trying to send an image on instagram using flutter with the social_share package.
My issue is that i'm new with flutter and i don't find a proper way to send a path to the function SocialShare.shareInstagramStory(imageFile.path, "#ffffff", "#000000", "https://deep-link-url");
My images are located in the root of the flutter project in assets/images/my_images.
When i try to put the path SocialShare.shareInstagramStory("assets/images/my_image.jpeg");
i guess the phone doesn't know this path since it's looking on his own directory
hope i'm clear enough and thanks for your time!
Edit : here's my code:
onPressed: () async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = File(tempDir.path);
final byteData = await rootBundle.load('assets/images/img_1.jpeg');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
await SocialShare.shareInstagramStory(file.path);
Navigator.of(context).pop();
},
i got this error :
E/flutter (12761): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.app/cache' (OS Error: Is a directory, errno = 21)
So basically, what you need to do is convert your asset image into a file, then create a temporary path and store this file there. Then you can reference that path
We need to create a temporary path with this plugin https://pub.dev/packages/path_provider
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Then create a file
File file = File(tempDir.path+'/img_1.jpeg');//You might have to play arounf with the /. Remove it or put it in the end to see what works
Now we need to write the image to this
final byteData = await rootBundle.load('assets/images/img_1.jpeg');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
Now you can use this temporary path to do what you want
Im trying to make an app that will open the camera and take a photo and load it using flutter. First I used pickImage but I got this message 'pickImage' is deprecated and shouldn't be used. Use imagePicker.getImage() method instead..
so I used getImage method...
_openCamera(BuildContext context) async{
var picture =await ImagePicker().getImage(source: ImageSource.camera);
this.setState(() {
imageFile= picture;
});
but then this error came when I used the below code
The argument type 'PickedFile' can't be assigned to the parameter type 'File'.
if(imageFile==null){
return Text("No selected Image");
}else{
Image.file(imageFile,width: 400,height: 400);
}
Please help me .I'm very new to flutter
PickedFile, which is the type returned by imagePicker, is a type specific to image_picker package, hence it's not the same as File.
You can do the following to convert it to File:
Image.file(File(imageFile.path), width: 400, height: 400);
I am using image_picker 0.6.7+17 library in order to take an image using the phone camera.
I am using an android device and not an ios device.
A problem
It seems like that getImage method is not defined, I took this exact code from the docs:
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
}
I am getting this error:
lib/pickers/image_picker.dart:17:37: Error: The method 'getImage' isn't defined for the class
'ImagePicker'.
- 'ImagePicker' is from 'package:chat_app/pickers/image_picker.dart'
('lib/pickers/image_picker.dart').
Try correcting the name to the name of an existing method, or defining a method named 'getImage'.
final pickedFile = await picker.getImage(source: ImageSource.camera);
^^^^^^^^
What I have done so far:
Added the dependency to my pubspec.yaml: file
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.0
cloud_firestore: 0.13.5
firebase_auth: 0.16.1
image_picker: ^0.6.7+17
Added android:requestLegacyExternalStorage="true" to the AndroidManifest.xml file
Also Imported import 'package:image_picker/image_picker.dart' to use this library
What could be the problem?
You are trying to use an old API with a plugin version that specifies to use the new API.
Old API
File image = await ImagePicker.pickImage(...)
New API
final _picker = ImagePicker();
.
.
.
PickedFile image = await _picker.getImage(...)
Are you sure the ImagePicker you are using is not the one from this package:chat_app/pickers/image_picker.dart ? Maybe there is a class name conflict and you must rename your own ImagePicker class
I got the same problem I solved by defining the picker like that
final picker = ImagePicker();
and then use this
Future<void> _chooseImage() async {
var pickedFile = await picker.getImage(source: ImageSource.gallery);
}
1)you may forget import the library of "image picker":import 'package:image_picker/image_picker.dart';
2)or import wrong library with the same name with some differences in the syntax of Sentence
Just upgrade to latest version of image_picker.
And replace getImage by pickImage bcz getImage has deprecated.