Flutter Json Encode List

前端 未结 3 820
闹比i
闹比i 2020-12-11 03:02

How to encode list to json?

This is my class for Json.

class Players{
  List players;

  Players({this.players});

  factory Players.fr         


        
3条回答
  •  不思量自难忘°
    2020-12-11 03:12

    Add toJson method to your Player class:

    Map toJson(){
      return {
        "name": this.name,
        "imagePath": this.imagePath,
        "totalGames": this.totalGames,
        "points": this.points
      };
    }
    

    Then you can call jsonEncode on the list of players:

    String encoded = jsonEncode(players) // this will automatically call toJson on each player
    

提交回复
热议问题