3

In a Flutter app I have a dart file located at FlutterTest\sandbox\lib\my_widget\my_widget.dart.

And I have an image located at FlutterTest\sandbox\lib\my_widget\imgs\myImage.png.

From my_widget.dart how can I point a file to this image, something along the lines of new File("???\my_widget\myImage.png")?

Just to clarify, in Java I could do something like MyClass.class.getResource("myImage.png").toString().

By the way if I print(Directory.current.path) it gives me simply /. And if I try to list the contents of this directory it tells me: "Directory listing failed, path = '/' (OS Error: Permission denied, errno = 13)". So I don't actually know where the current directory is.

2
  • way you didn't store the images in the asset directory so you can get to them easily in flutter Commented Apr 7, 2018 at 0:27
  • 2
    For widget tests we have some reference images that show how the widgets should look like, and I wanted to simply let the developers put them in the test directory, without having to list them as assets. It's not for the real production app itself. Besides that I was wondering where do you save and retrieve images that you download from the Internet and want to cache locally. Commented Apr 7, 2018 at 18:44

1 Answer 1

2

You can directly access the file system but due to differences in android and iOS you'll probably end up using the path provider plugin. Also, see this flutter cookbook as they call it.

But if all you're trying to do is access a file from your build, you'll want to define it as an asset in your pubspec, and then access it using Flutter's AssetBundle (or Image.asset) rather than directly using File or Image.file. See the flutter documentation on assets.

Sign up to request clarification or add additional context in comments.

3 Comments

Judging from the slew of questions you've asked about opening files and getting paths, it seems as though you might be trying too hard with flutter, for lack of a better term. If you're running into roadblocks doing basic things like displaying images, it’s more likely you've missed something rather than flutter not supporting it. I'd like to recommend that you read and follow through a couple of the tutorials on the website this layout one for example as it will really save you time in the long run =)
You're right, I've been quite antagonistic and I will stop. I apologize =D. The answer to this question is readily available on the flutter website but the others have all been reasonable & well thought out. I am slightly curious about the end result you're working towards though!
For widget tests we have some reference images that show how the widgets should look like, and I wanted to simply let the developers put them in the test directory without having to list them one by one as assets. It's not for the real production app itself. There is a feature request for Flutter to import whole directories without having to specify file by file, and it seems they are actually working on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.