Flutter, how can i access cached file in android?

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?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//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.
</code>
<code>//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. </code>
//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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật