简体   繁体   中英

flutter get data from api and assign to array

List data;

i want to show response but when i try to assign jasonResponse to data i get this error

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'List' is not a subtype of type 'Map'

so what should i do

how to assign array of object to data

Future<String> getJsonData() async{

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {'sale_id': sharedPreferences.getInt("token")};

    var jsonResponse;
    var response = await http.post(
      "http://",
      body: json.encode(data),
      headers: {
        "Content-Type": "application/json"

      },
    );

    if (response.statusCode == 200) {
      jsonResponse = json.decode(response.body);
      print('jsonRespnse');
      print(jsonResponse['developer_list']);
      if (jsonResponse != null) {
         setState(() {

          data = jsonResponse['developer_list'];

         });

Your data is a type Map but you are assigning a type List .

Simply, add a key with your value, like below:

data['developer_list'] = jsonResponse['developer_list'];

Make sure to access that list by data['developer_list']

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