flutter型别'List< dynamic>'不是型别'FutureOr〈List>'的子型别< Product>

gzjq41n4  于 2022-11-17  发布在  Flutter
关注(0)|答案(2)|浏览(102)

我得到FutureBuilder快照错误当我解析我的JSON我得到了:
类型'List'不是类型'FutureOr '的子类型。是产品模型错误还是解析错误?
我的代码

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");
                }
              }),

相关问题