Reputation: 746
I am trying to save the image names which i am getting from the imagepicker to the firestore database
How i declared it
List<File> _imageName = [];
Stored the name
_imageName.add(File(Path.basename(pickedFile.path)));
Output of _imageName
[File: 'image_picker2088970390141772511.jpg', File: 'image_picker4765409869719291061.jpg', File: 'image_picker8815762890677993639.jpg', File: 'image_picker2293914669929574097.jpg']
How i tried to saved it to the firestore
"imageUrl": FieldValue.arrayUnion(imageUrl),
Error
E/flutter (30296): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Invalid argument: Instance of '_File'
Upvotes: 0
Views: 154
Reputation: 4577
Hi there you cannot save a File type directly to Firestore, you need to save it as a string. for example if you just want the name of the file try extracting it from your path when creating your array,
imageName.add(File(Path.basename(pickedFile.path)).path.split('/').last)
or since you already had the path.
imageName.add(pickedFile.split('/').last)
Finally if you need to actually save the file and not only the path, than you may need to use firebase storage for that.
You can also create an extension to just use a .name methods on your files. For example:
import 'dart:io';
extension FileExtention on FileSystemEntity{
String get name {
return this?.path?.split("/")?.last;
}
}
And then you would do something like this:
imageName.add(File(Path.basename(pickedFile.path)).name)
Upvotes: 1