简体   繁体   中英

get data from array in flutter

thanks for checking out my question i have the following json data

array(568) {
  [0]=>
  array(11) {
    ["ID"]=>
    string(6) "000001"
    ["Portrait"]=>
    string(12) "BaseGoku.png"
    ["Name"]=>
    string(42) "[The Saiyan who grew up on Earth] Son Goku"
    ["Series"]=>
    string(11) "Dragon Ball"
    ["MaxRarity"]=>
    string(6) "6 Star"
    ["Type"]=>
    string(4) "Blue"
    ["Class"]=>
    string(4) "Tank"
    ["Era"]=>
    string(4) "1980"
    ["ReleaseDate"]=>
    string(10) "2018-03-28"
    ["Farmable"]=>
    string(1) "0"
    ["Method"]=>
    string(17) "Trade Medal Store"
  }
  [1]=>
  array(11) {
    ["ID"]=>
    string(6) "000003"
    ["Portrait"]=>
    string(8) "Goku.png"
    ["Name"]=>
    string(8) "Son Goku"
    ["Series"]=>
    string(11) "Dragon Ball"
    ["MaxRarity"]=>
    string(6) "6 Star"
    ["Type"]=>
    string(6) "Yellow"
    ["Class"]=>
    string(3) "DPS"
    ["Era"]=>
    string(4) "1980"
    ["ReleaseDate"]=>
    string(10) "2018-03-28"
    ["Farmable"]=>
    string(1) "0"
    ["Method"]=>
    string(14) "Standard Gacha"
  } etc

i have been tring to decode this and pull the data using a future

Future<Users> fetchInfo() async {
  final response = await http.get(jsonplaceholder);
  final jsonresponse = json.decode(response.body);

  return Users.fromJson(jsonresponse[8]);

}

but i keep getting the error "Error FormatException: Unexpected character(at character 1) array(568) not to sure what im doing wrong as i am new to flutter any help would be appreciated thanks

heres the users model

class Users {
 final int userID;
  final String name;
 final String portrait;
  final String series;


  Users({this.name, this.userID, this.portrait, this.series});

  factory Users.fromJson(Map<String, dynamic> usersjson)=> Users(

      name: usersjson["name"],

  );
}

First of all are you sure this is the json data you're getting cause it should be like

[
  {
    "ID": "000001"
    "Name": "[The Saiyan who grew up on Earth] Son Goku",
    "Portrait": "BaseGoku.png",
    "Series": "Dragon Ball",
    "MaxRarity": "6 start",
    "Type": "Blue",
    "Class": "Tank",
    "Era": "1980",
    "Release Date": "2018-03-28",
    "Farmable": "0",
    "Method": "Trade Medal Store"
  }
]

Flutter expects Map type for json.decode . If it's a public api can you please post it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM