Unhandled Exception: InternalLinkedHashMap' is not a subtype of type 'List

前端 未结 9 733
夕颜
夕颜 2020-12-15 16:04

I am trying to get the JSON response from the server and output it to the console.

Future login() async {
    var response = await http.get(
            


        
相关标签:
9条回答
  • 2020-12-15 16:32

    Here are 2 common ways this could go wrong:

    1. If your response is a json object like

      [
          {
            key1: value1,
            key2: value2,
            key3: value3,
          },
          {
            key1: value1,
            key2: value2,
            key3: value3,
          },
      
          .....
      ] 
      

      Then, we use data[0]["name"], not data[0].name Unless we cast to an object that has the name property, we cannot use data[0].name

      We cast like this data = json.decode(response.body).cast<ObjectName>();

      ObjectName can be whatever object you want (Inbuilt or Custom). But make sure it has the name property

    2. If your response is a JSON object like

      {
          dataKey: [
            {
              key1: value1,
              key2: value2,
              key3: value3,
            } 
          ] 
      }
      

      Then json.decode will return a Map, not a List

      Map<String, dynamic> map = json.decode(response.body);
      List<dynamic> data = map["dataKey"];
      print(data[0]["name"]);
      
    0 讨论(0)
  • 2020-12-15 16:36

    as doesn't change the type, it's just an assertion.

    You need to use map['eventType'].cast<String, dynamic>() or Map<String, dynamic>.from(map['eventType'])

    You can also solved by this way:

    Map<String, dynamic> myMap = new Map<String, dynamic>.from(/*Your Source*/ );
    
    0 讨论(0)
  • 2020-12-15 16:43

    If your working with Firebase Cloud,make sure that you're not trying to add multiple data with same DocumentID;

    firestore.collection('user').document(UNIQUEID).setData(educandos[0].toJson()).
    
    0 讨论(0)
提交回复
热议问题