0

There are two Functions that I use to convert two String variables into JSON and from JSON.

String toJson() {
  Map<String,dynamic> map = {'name': name,'count':checkListCount,'description':description,};
  return jsonEncode(map)
}

fromJson(String context){
  Map<String,dynamic> map = jsonDecode(contents)
  name = map['name'];
  description = map['description'];
  return '0';
}

How i can use this to functions to covert List? There is my list

List<CheckListPoint> checkListPoints = [];

CheckListPoint{
  bool correctly = false;
  bool passed = false;
  String requirement = '';
}

variables that I have in CheckListPoint will change by the user later in the app.

1

2 Answers 2

1

i am not exactly getting which key will give you the list of data from your question, but suppose you have a response in with "data" key is giving you list of items, then you can add from JSON with ->

    data : List<User>.from(json["data"].map((x) => User.fromJson(x))),

and to convert it to JSON you need

    "data": List<dynamic>.from(data.map((x) => x.toJson())),

i hope this is what you are asking for

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

Comments

0

I did a very similiar thing when fetching messages from firebase. It's the same as parsing from JSON since both values are dynamic.

Here I convert a DataSnapshot to a List

Future<List<Message>> getMessages(DataSnapshot snapshot) async {
    List<Message> msgs = List.empty(growable: true);
    if(snapshot.value != null) {
      Map<dynamic, dynamic> messages = snapshot.value;
      messages.forEach((key, value) {
        msgs.add(Message.fromFirebase(value));
      });
    }
    return msgs;
 }

And here is the Message class:

class Message {
  String message;
  String senderId;
  int time;

  Message.fromFirebase(Map<dynamic, dynamic> json) :
      message = json["message"],
      senderId = json["senderId"],
      time = json["time"];
}

Comments

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.