gpt4 book ai didi

flutter - 快照错误类型 'int' 不是类型 'String' 的子类型

转载 作者:IT王子 更新时间:2023-10-29 07:18:34 25 4
gpt4 key购买 nike

我正在尝试使用 FutureBuilder 获取数据并显示。 fetch 调用后显示 type 'int' is not a subtype of type 'String'

这是我的 json 模型:

class Featured {
final int id;
final String shop_type;
final String name;
final String name_bn;
final String price;
final String price_bn;
final bool old_price;
final String old_price_val;
final String old_price_val_bn;
final String image;

Featured(
{this.id,
this.shop_type,
this.name,
this.name_bn,
this.price,
this.price_bn,
this.old_price,
this.old_price_val,
this.old_price_val_bn,
this.image});

factory Featured.fromJson(Map<String, dynamic> json) {
return Featured(
id: json['id'],
shop_type: json['shop_type'],
name: json['name'],
name_bn: json['name_bn'],
price: json['price'],
price_bn: json['price_bn'],
old_price: json['old_price'],
old_price_val: json['old_price_val'],
old_price_val_bn: json['old_price_val_bn'],
image: json['image'][0]['image_mobile_feature_list']);
}
}

和主要部件

class Products extends StatefulWidget {
@override
_ProductsState createState() => _ProductsState();
}

class _ProductsState extends State<Products> {

Future<List<Featured>> getProducts() async {
final response = await http.get(Uri.encodeFull(kIndexFeaturedUrl));
List<Featured> list = List();

if (response.statusCode == 200) {
var data = json.decode(response.body);
var rest = data["results"] as List;
list = rest.map<Featured>((json) => Featured.fromJson(json)).toList();
return list;
} else {
throw Exception('Network connection failed');
}
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getProducts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
'test',
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
);

} else if (snapshot.hasError) {
return Center(child: Text("Error: ${snapshot.error}"));
}

return Center(child: CircularProgressIndicator());
},
);
}
}

更新0:数据样本

{id: 1, name: example, name_bn: example, price: 88, price_bn: 80, old_price: false, old_price_val: null, old_price_val_bn: null, image: [{image_mobile_feature_list: https://xyz/media/CACHE/images/shop/product/IMG_1057_-_Copy_3FW1ZM7/1781f5617cc7efe708439802736d8ea7.webp, image_mobile_product_details: https://xyz/media/CACHE/images/shop/product/IMG_1057_-_Copy_3FW1ZM7/fe2589e7b7df5e542aaf37cb52c44523.webp}]}

更新 1:

I/flutter ( 1961): int//id
I/flutter ( 1961): int//价格
I/flutter ( 1961): 字符串//price_bn
I/flutter ( 1961): 空//old_price_val

最佳答案

toString() 添加到您的 price 属性。

price: json['price'].toString(),

我敢打赌 old_price_val 当它不为空时也是一个 int。我也会为此调用 toString()

关于flutter - 快照错误类型 'int' 不是类型 'String' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56544300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com