Home > Net >  flutter type 'List<dynamic>' is not a subtype of type 'FutureOr<List<Prod
flutter type 'List<dynamic>' is not a subtype of type 'FutureOr<List<Prod

Time:11-14

i got FutureBuilder snapshot error when i parsing my json i got:

type 'List' is not a subtype of type 'FutureOr<List>' is it my Product model error or parsing error?

my code

 late Future<List<Product>> productFuture = getProducts();

  static Future<List<Product>> getProducts() async {
    var url = '${Constants.API_URL_DOMAIN}action=catalog&category_id=$id';
    final response = await http.get(Uri.parse(url));
    final body = jsonDecode(response.body);
    print(body['data']);
    return body['data'].map((e)=>Product.fromJson(e)).toList();
  }

FutureBuilder<List<Product>>(
              future: productFuture,
              builder: (context, snapshot) {
                print(snapshot);
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(child: CircularProgressIndicator());
                } else if (snapshot.hasData) {
                  final catalog = snapshot.data;
                  return buildCatalog(catalog!);
                } else {
                  print('SNAPSOT DATA ${snapshot.data}');
                  return Text("No widget to build");
                }
              }),

CodePudding user response:

Use List.from

return List.from(body['data'].map((e)=>Product.fromJson(e)));
static Future<List<Product>> getProducts() async {
    var url = '${Constants.API_URL_DOMAIN}action=catalog&category_id=$id';
    final response = await http.get(Uri.parse(url));
    final body = jsonDecode(response.body);
    print(body['data']);
    return List.from(body['data'].map((e)=>Product.fromJson(e)));
  }

CodePudding user response:

Try to convert all List<Product> --> List<dynamic>

  • Related