Error: List is not a subtype of type Map

后端 未结 5 1949
一生所求
一生所求 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条回答
  •  Happy的楠姐
    2020-12-09 09:10

    Check out this Answer

     Future> 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>(
        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: [
                             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

提交回复
热议问题