Import a local Json File

1,662 views
Skip to first unread message

Michael Tawiah Sowah

unread,
Sep 8, 2017, 1:12:20 PM9/8/17
to Flutter Dev
How do I Import a local Json file in order to use the data

Andrew Wilson

unread,
Sep 8, 2017, 2:23:57 PM9/8/17
to Michael Tawiah Sowah, Flutter Dev
You'll probably want to do something like this in your code:

 File file = new File(_kConfigFile);

    final Map<String, List<Map<String, String>>> data =
        file.existsSync()
            ? convert.JSON.decode(
                file.readAsStringSync(),
              )
            : <String, List<Map<String, String>>>{};


And then use the resulting json object to create widgets.

On Fri, Sep 8, 2017 at 10:12 AM, Michael Tawiah Sowah <michael...@gmail.com> wrote:
How do I Import a local Json file in order to use the data

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Tawiah Sowah

unread,
Sep 8, 2017, 2:26:46 PM9/8/17
to Flutter Dev
am confused...a little clarification her please


On Friday, September 8, 2017 at 6:23:57 PM UTC, Andrew Wilson wrote:
You'll probably want to do something like this in your code:

 File file = new File(_kConfigFile);

    final Map<String, List<Map<String, String>>> data =
        file.existsSync()
            ? convert.JSON.decode(
                file.readAsStringSync(),
              )
            : <String, List<Map<String, String>>>{};


And then use the resulting json object to create widgets.
On Fri, Sep 8, 2017 at 10:12 AM, Michael Tawiah Sowah <michael...@gmail.com> wrote:
How do I Import a local Json file in order to use the data

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Andrew Wilson

unread,
Sep 8, 2017, 2:32:38 PM9/8/17
to Michael Tawiah Sowah, Flutter Dev
1. You read the file using File (_kConfigFile is the path to the file).
2. You convert the String read from File to json via convert.JSON.decode (you'll need to import dart.convert).
3. Use the resulting Map or List (depending on the contents of the json file) however you want.

-Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

Michael Tawiah Sowah

unread,
Sep 8, 2017, 2:42:40 PM9/8/17
to Flutter Dev
Do I need to declare the file path in pubspec.yaml before I can use it in the File(path);
File file = new File(path); and I keep getting undefined class 'File '

Seth Ladd

unread,
Sep 8, 2017, 3:14:47 PM9/8/17
to Michael Tawiah Sowah, Flutter Dev, Sarah Zakarias
+Sarah who I believe just added some support for reading in assets?

Michael Thomsen

unread,
Sep 8, 2017, 3:20:29 PM9/8/17
to Seth Ladd, Sarah Zakarias, Michael Tawiah Sowah, Flutter Dev
I think we might not support getting a path to an asset right now? I opened a bug for this a few days ago:

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

Michael Tawiah Sowah

unread,
Sep 8, 2017, 3:35:04 PM9/8/17
to Flutter Dev

where can I get to read

Andrew Wilson

unread,
Sep 8, 2017, 4:00:31 PM9/8/17
to Michael Tawiah Sowah, Flutter Dev
If you include the file in assets you can read it with: AssetBundle.loadString(...)


To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

Michael Thomsen

unread,
Sep 11, 2017, 6:58:19 AM9/11/17
to Andrew Wilson, Michael Tawiah Sowah, Flutter Dev
Yes, please ignore my previous answer (which was for cases where you need the file path, not your case where you need the file data).

I added some sample code here:

This assumes you have the data in data/states.json, with contents similar to this:

Michael Tawiah Sowah

unread,
Sep 11, 2017, 7:01:24 AM9/11/17
to Flutter Dev
Thank you so much Michael.
Now I would appreciate also some help on this if you could

Michael Thomsen

unread,
Sep 11, 2017, 7:11:32 AM9/11/17
to Michael Tawiah Sowah, Flutter Dev
Sorry, don't have a good answer for that one. But I think you might have better luck getting an answer if you post your question on StackOverflow:


To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

Michael Tawiah Sowah

unread,
Sep 11, 2017, 7:16:29 AM9/11/17
to Flutter Dev
Tried that with an earlier question and nothing

Michael Tawiah Sowah

unread,
Sep 12, 2017, 8:25:15 AM9/12/17
to Flutter Dev
Hi Michael, worked well, I was wondering eg: I have a search input at the top of the list and I want to filter through the list as the user types.


On Monday, September 11, 2017 at 10:58:19 AM UTC, Michael Thomsen wrote:

Si...@prayuta.com

unread,
Sep 13, 2017, 2:33:46 AM9/13/17
to Flutter Dev
can u please give example for this

Michael Thomsen

unread,
Sep 13, 2017, 4:53:06 AM9/13/17
to Siva Vuppala, Flutter Dev
This sounds like a perfect question for StackOverflow; have you tried that?

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

Si...@prayuta.com

unread,
Sep 14, 2017, 3:47:11 AM9/14/17
to Flutter Dev
add file to assets
add path to assets in pubspec.yaml file
reading  the data  
for example
 var a = await rootBundle.loadString('assets/countries.json');
var b = JSON.decode(a);
print(JSON.decode(a));
b.forEach((Map map) =>
names.add(new NameData.fromJson(map)));
return "xyzzy";
}

kya...@yathit.com

unread,
Sep 15, 2017, 9:09:47 PM9/15/17
to Flutter Dev
I also have same problem reading data file, that work both app and testing.

I found AssetBundle does not work in test.

Ian Hickson

unread,
Sep 16, 2017, 11:15:29 PM9/16/17
to kya...@yathit.com, Flutter Dev
We don't create any assets bundles for testing. You might be able to read from disk using the dart:io APIs, though.

--
Ian Hickson

😸

kufah...@gmail.com

unread,
Apr 11, 2018, 10:37:46 AM4/11/18
to Flutter Dev
To anyone who is still searching for a solution:

- I had a JSON file stored in {project_root}/assets/json/quotes.json
- I tried using File.readAsSync() method to put the contents into a Map variable after decoding it: No luck. When I would run my code by using https://marketplace.visualstudio.com/items?itemName=formulahendry.CodeRunner, it would work perfectly. But if I tried using flutter run, it spit out a "cannot find file" error.

- Finally I followed this tutorial and edited the code to suit my purpose. You can see my json file (it's minified) here and the code that gets a random quote from value from the json list + puts the whole thing into a ListView() here.

Note: I just discovered that Bram (same guy as above) seems to have created a nice tutorial that would do what I wanted to do using File(), but he adds a dependency so he can get the right directory. See the code here: https://github.com/bramvbilsen/Flutter-JSON-Storage-Tutorial/blob/master/lib/main.dart ad the vid here: https://www.youtube.com/watch?v=jVVCHzkI8as

Hope this helps.

ma...@didierboelens.com

unread,
Apr 19, 2018, 10:40:38 AM4/19/18
to Flutter Dev
Hi Kifah,

Very small comment on your code, if I may.

JSON.decode has been deprecated and we should now use json.decode, instead.

Regards, 

Reply all
Reply to author
Forward
0 new messages