this is a simple logic that the app gets data from open api and stores images in cache, showing an image in a screen a user selected. When a user decides to save it, I intended to save it in a document directory.
What’s drove me crazy was that the image from cached file path works perfectly in a screen, but when I tried to save it in document directory, it throws an error that the path doesn’t exist.
Why File(addMedicineViewModel .cachedFilePath.value)
in image widget works, but not in
final vmfile = File(addMedicineViewModel.cachedFilePath.value);
in save button?
Even when an image widget shows the file from the same path, why can’t I access to it from another method? Is it like it’s impossible to try multiple access to a cached file?
//screen
Obx(
() => Center(
child: InteractiveViewer(
minScale: 0.8,
clipBehavior: Clip.none,
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(8.0),
border: Border.all(
color: Colors.white, width: 2),
boxShadow: const [
BoxShadow(
color: Colors.black26,
blurRadius: 4.0,
offset: Offset(0, 2),
),
],
image: DecorationImage(
image: FileImage(File(addMedicineViewModel
.cachedFilePath.value)),
fit: BoxFit.cover,
),
),
// child: Image.network(medicine.imgDownloadUrl),
)
// on save button in the same screen
GestureDetector(
onTap: () async {
final file = File(cachedFilePath!);
final vmfile =
File(addMedicineViewModel.cachedFilePath.value);
print(
'file exists ? in add medi screen : ${await file.exists()}');
print(
'vmfile exists ? in add medi screen : ${await vmfile.exists()}');
// in view model
Future<String> copyCacheToDocument() async {
final file = File(cachedFilePath.value);
print('cached file path in copyCachedToDocument: ${cachedFilePath.value}');
print('file exists ? in copyCachedToDocument : ${await file.exists()}');
// print logs
I/flutter ( 1928): file exists ? in add medi screen : false
I/flutter ( 1928): vmfile exists ? in add medi screen : false
I/flutter ( 1928): file exists ? in saveMedicine : false
I/flutter ( 1928): cached file path in copyCachedToDocument: /data/user/0/com.example.vital/cache/libCachedImageData/cb7e6020-4f36-11ef-ae08-c145a77f1e18.download. //this path is used in showing the image from screen
I/flutter ( 1928): file exists ? in copyCachedToDocument : false
this almost drove me crazy for a couple of days and i still don't know why.
I tried to pass the file from cached file path and to get File() instance but it didn't work. I expected to be able to access the cached file path wherever i needed like in view model or another widget in the same screen.