快照错误类型“int”不是“String”类型的子类型

棱柱

我正在尝试获取数据并使用FutureBuilder. 获取后调用它显示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 // price I/flutter ( 1961): String // price_bn I/flutter ( 1961): Null // old_price_val

雨果·帕索斯

添加toString()到您的price财产。

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

我敢打赌old_price_valint当它不为空时也是一个我也会呼吁toString()它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Dart中的'int'类型不是'String'类型的子类型错误

'String'类型不是'index'错误的'int'类型的子类型

'String'类型不是'index'错误的'int'类型的子类型

int 类型不是 String 类型的子类型

“String”类型不是“int”类型的子类型

Flutter错误“不是'String'类型的子类型

颤振错误:类型转换中类型'int'不是类型'String'的子类型

错误类型'_InternalLinkedHashMap <String,dynamic>'不是'String'类型的子类型

类型“ String”不是预期类型“ int”的子类型

TypeError(类型“int”不是“String”类型的子类型)

'int' 类型不是 Flutter 上的 'String' 类型的子类型

未处理的异常:类型'int'不是类型'String'的子类型

firestore 数组:“String”类型不是“int”类型的子类型

未处理的异常:“String”类型不是“int”类型的子类型

错误:在flutter中使用fromjson时,类型'String'不是'index'类型'int'的子类型

如何修复 FutureBuilder 上的“int”类型不是“String”类型错误的子类型?

为什么解析方法抛出“int”类型不是“String”类型错误的子类型?

出现此错误:在Flutter应用上,类型'String'不是'index'的类型'int'的子类型

颤抖:错误:类型'String'不是'encrypted'的'Encrypted'类型的子类型

“ int”类型不是“ double”类型的子类型

“int”类型不是“double”类型的子类型

“Null”类型不是“int”类型的子类型

'String'类型不是'File'类型的子类型

'String'类型不是'bool'类型的子类型

'String'类型不是'double'类型的子类型

“double”类型不是“String”类型的子类型

如何在类型转换错误中修复类型'_InternalLinkedHashMap <String,String>'不是'String'类型的子类型

错误:List <dynamic>不是Map <String,dynamic>类型的子类型

错误“_InternalLinkedHashMap<String, dynamic>”不是“Iterable<dynamic>”类型的子类型