30

I am using image_picker to get some images from gallery, and I saved the path of the images in a list, like:

/private/var/mobile/Containers/Data/Application/E633FB78-77D3-4913-B37A-496BFBAAD00C/tmp/image_picker_EA6D2836-3EAD-438C-AEEE-21CB3500ED28-9410-00000706FA96031D.jpg

how can I open the image from the path in flutter?

I tried Image.file, but it doesn't work, please help.

CybeX
  • 2,060
  • 3
  • 48
  • 115
Niccolo
  • 933
  • 4
  • 12
  • 17

6 Answers6

56

The Image class has a file constructor for that

https://api.flutter.dev/flutter/widgets/Image/Image.file.html

Image.file(File(path))
julemand101
  • 28,470
  • 5
  • 52
  • 48
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 3
    I tried Images.file(path), but I got "Another exception was thrown: type 'String' is not a subtype of type 'File'" – Niccolo Sep 29 '18 at 14:53
  • thanks for your answer! now i got this " Class 'Image' has no instance getter 'url'.", do you have any clue for this? – Niccolo Sep 29 '18 at 15:05
  • No, that sounds like it's from other code that what I provided in my answer. I would need more information. – Günter Zöchbauer Sep 29 '18 at 15:06
  • 1
    i think the issue is from the carousel_pro package, thanks for your help! – Niccolo Sep 29 '18 at 15:16
  • 5
    Hey, I tried to use `Image.file(....)` on `content://media/external/images/media/168155` string and it didn't work. someone know how to use or convert this kind of path ? – noam aghai Apr 02 '19 at 08:29
  • 1
    Flutter can't read `content://...` urls directly. You need to resolve that in Java/Kotlin and pass the result to Dart. I don't know if there is a ready-to-use plugin available in pub.dartlang.org for that. – Günter Zöchbauer Apr 02 '19 at 08:33
12

For those who have tried Image.file(File(path)) and have an issue with arguments, make sure you have imported dart:io, not dart:html, because sometimes it imports automatically.

Ted
  • 129
  • 1
  • 4
7

I had the same problem, and I solved it with the following code:
Image.file(new File(StringPathVariable)

Artemis
  • 2,553
  • 7
  • 21
  • 36
2

If I remember correctly Image.file() can only accept ImageProvider<Object>, and apparently File(imagePath) is categorized as Image type

So if Image.file(File(imagePath)) fail, you can add .image at the end to turn it into ImageProvider<Object>, so the following code should work:

Image.file(File(imageUri)).image

  • Image.file(File(imageUri)).image does not work. It is marked as sytax error ecen after importing dart:io – Elmar Nov 18 '22 at 08:02
2

Suggested solutions do not work. Working solution in 2022:

import 'dart:io';

     Container(child:
            Image.file(File(savedImage!.path))),
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Elmar
  • 2,235
  • 24
  • 22
0

For those who have tried Image.file(File(path)) and have an issue with argument. The issue is because of imported dart:HTML. Try to import 'dart:io'. Then it will work fine