首页 > 解决方案 > E/flutter:[错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:“String”类型不是“int”类型的子类型

问题描述

** 如何解决上述错误此错误导致以下代码.......................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ...... **

product.dart

  Future<void> getHomeAchiveData() async {
    List<Productss> newList = [];
    QuerySnapshot featureSnapShot =
    await FirebaseFirestore.instance.collection("homearchive").get();
    featureSnapShot.docs.forEach(
          (element) {
        featureData = Productss(
            image: element.data()["image"],
            name: element.data()["name"],
            price: element.data()["price"],
        description: element.data()["description"]);
        newList.add(featureData);
      },
    );
    homeAchive = newList;
    notifyListeners();
  }

  Future<void> getHomeAchiveData() async {
    List<Productss> newList = [];
    QuerySnapshot featureSnapShot =
    await FirebaseFirestore.instance.collection("homearchive").get();
    featureSnapShot.docs.forEach(
          (element) {
        featureData = Productss(
            image: element.data()["image"],
            name: element.data()["name"],

            price: element.data()["price"],// error cause from  this line**
     
           description: element.data()["description"]);
        newList.add(featureData);
      },
    );
    homeAchive = newList;
    notifyListeners();
  }

Error in line 
 price: element.data()["price"],// error cause from  this line**




 

类别模型.dart

categorymodel.dart
        
        class Productss {
          final String name;
          final String image;
          final String image2;
          final String image3;
          final String image4;
          final String description;
          final String shortInfo;
          final int price;
          final int count;
        
          Productss( {this.count,this.shortInfo, this.image2,  this.image3, this.image4,@required this.description,@required this.image, @required this.name, @required this.price});
        }

标签: flutterdart

解决方案


代替

element.data()["price"],

到:

int.parse(element.data()["price"],)

推荐阅读