Home > database >  From .dart to .json
From .dart to .json

Time:09-02

Currenly using a .dart like this

const aList = <Map<String, dynamic>>[
  {"url_helper": "aListFolder", "name": "a2a"},
  {"url_helper": "aListFolder", "name": "a2b"},
];

const bList = <Map<String, dynamic>>[
  {"url_helper": "bListFolder", "name": "b2a"},
  {"url_helper": "bListFolder", "name": "b2b"},
];

const cList = <Map<String, dynamic>>[
  {"url_helper": "cListFolder", "name": "c2a"},
  {"url_helper": "cListFolder", "name": "c2b"},
];



const abList = [...aList, ..bList];

const allList = [...aList, ...bList, ...cList];

So I was using it in this way.

                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => const UserPicked(
                            userchoise: abList, // user picked a and b list.
                          )),
                );

Now, I put all the data in a .json

So it looks like this.

{
 "aList" : [
    {"url_helper": "aListFolder", "name": "a2a"},
    {"url_helper": "aListFolder", "name": "a2b"}
],
  "bList" : [
    {"url_helper": "bListFolder", "name": "b2a"},
    {"url_helper": "bListFolder", "name": "b2b"}
],
"cList" : [
    {"url_helper": "cListFolder", "name": "c2a"},
    {"url_helper": "cListFolder", "name": "c2b"}
],
}

How do I do the samething but with .json?


Future readJson() async {
final String response = await rootBundle.loadString('assets/json.json');
final data = await json.decode(response);
}

Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => const UserPicked(
                            userchoise: data['aList'], // user picked a list.
                          )),
                );

Is it possible to do something like this, or should I still keep the same way using the .dart ?

Thanks for any help!

CodePudding user response:

Don't forget to return your data, it's always good to declare a return type if you know what you want to return from there. This will make Flutter tell you the error.

Future<Map<String, dynamic>> readJson() async {
  final String response = await rootBundle.loadString('assets/json.json');
  final data = await json.decode(response);
  return data;
}

CodePudding user response:

Working with statically typed values is always easier than dynamic types.

Thats one of the reason why using the "dart" approach as you put it is better.

If you have some JSON data to work with ill recommend to convert it to dart data types first by making a fromMap function.

This is an example of how I do it:

class Roles {
  final String? id;
  final String name;

  Roles({this.id, required this.name});

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
    };
  }

  factory Roles.fromMap(Map<String, dynamic> map) {
    return Roles(
      id: map['id'] != null ? map['id'] : null,
      name: map['name'],
    );
  }

  String toJson() => json.encode(toMap());

  factory Roles.fromJson(String source) => Roles.fromMap(json.decode(source));
}

If this is what you are looking for please mark my answer as correct.

  • Related