Error: List is not a subtype of type Map

后端 未结 5 1940
一生所求
一生所求 2020-12-09 08:29

I am currently building an app to read data through an api and I am trying to parse a JSON api from JSON Placeholder.

I made a model class for the Users (users_futur

相关标签:
5条回答
  • 2020-12-09 08:48
    var streetsFromJson = parsedJson['streets'];
    List<String> streetsList = new List<String>.from(streetsFromJson);
    
    

    Thnx for https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51

    0 讨论(0)
  • 2020-12-09 08:51

    Just pay attention to the error

    List<dynamic> is not a subtype of type Map<String, dynamic>
    

    basically it means the data which you are receiving after hitting a webservice is in form of List but inside data class which you have used is of type Map .So, it is unable to parse the data due to structure mismatch as Map is defined in instead of List in data class.

    Recheck your Json response and corresponding PODO class

    0 讨论(0)
  • 2020-12-09 08:57

    API returns JSON array not json object so that is List not Map.

    i.e. User json is first element of Json Array.

    So to get first element use first index. Inside fetch Info update

    return Users.fromJson(jsonresponse[0]);

    0 讨论(0)
  • 2020-12-09 09:05

    Use

    List<dynamic> output = jsonDecode(yourJsonSource);
    

    instead of

    Map<String, dynamic> output = jsonDecode(yourJsonSource);
    
    0 讨论(0)
  • 2020-12-09 09:10

    Check out this Answer

     Future<List<ProductList>> getProductList() async {
      print("comes");
      String productURl= mainURL+'api/store/product-with-category/';
    
      final response = await http.get(productURl,headers:{"Content-Type": 
     "application/json"});
      List jsonResponse = json.decode(response.body);
      return jsonResponse.map((job) => new ProductList.fromJson(job)).toList();
      
    }
    

    Fetch function

        FutureBuilder<List<ProductList>>(
        future: getProductList(),
        builder: (context, snapshot) {
          print("snapshot");
          print(snapshot.data);
          if (snapshot.hasData) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: GridView.builder(
              itemCount: snapshot.data.length,
                gridDelegate:SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2,),
                itemBuilder:  (BuildContext context, int i){
                  return Card(
                    child: Container(
                      decoration: BoxDecoration(
                          border: Border.all(width: 0.5,color: Colors.grey)
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: <Widget>[
                             text18(snapshot.data[i].title, Colors.black, FontWeight.bold)
                          ],
                        ),
                      ),
                    ),
                  );
                }
            )
            );
          } else if (snapshot.hasError) {
            return Text("${snapshot.error}");
          }
    
    
          // By default, show a loading spinner.
          return CircularProgressIndicator();
        },
      ),
    

    just change URL and create model class

    0 讨论(0)
提交回复
热议问题