Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 类型';列表<;动态>';不是类型为';未来者<;列表<;CartProduct>&燃气轮机';_Firebase_Flutter_Google Cloud Firestore_Flutter Test - Fatal编程技术网

Firebase 类型';列表<;动态>';不是类型为';未来者<;列表<;CartProduct>&燃气轮机';

Firebase 类型';列表<;动态>';不是类型为';未来者<;列表<;CartProduct>&燃气轮机';,firebase,flutter,google-cloud-firestore,flutter-test,Firebase,Flutter,Google Cloud Firestore,Flutter Test,我有一个在android studio中运行良好的错误,现在我转到vscode,并在这个线程的标题中给出了这个错误。请在下面查找代码 省道 class CartProduct { double? totalPrice; String? userId; int? quantity; double? subtotal; String? id; ProductData? products; int? status; DateTime? date; CartProd

我有一个在android studio中运行良好的错误,现在我转到vscode,并在这个线程的标题中给出了这个错误。请在下面查找代码

省道

class CartProduct {
  double? totalPrice;
  String? userId;
  int? quantity;
  double? subtotal;
  String? id;
  ProductData? products;
  int? status;
  DateTime? date;

  CartProduct({
    //@required this.totalPrice,
    this.products,
    this.quantity,
    this.date,
    this.userId,
    this.id,
    this.subtotal,
    this.status,
  });
  CartProduct.fromDoucment(DocumentSnapshot document) {
    try {
      date = document.data()!['date'].cast<DateTime>();
      id = document.data()!['id'].toString();
      products = ProductData.fromMap(
          document.data()!['products']); // Here is where my issue is
      quantity = document.data()!['quantity'].cast<int>();
      subtotal = document.data()!['subtotal'].cast<double>();
      userId = document.data()!['userId'].toString();
      status = document.data()!['status'].cast<int>();
    } catch (e) {
      if (e is PlatformException) {
        print('error');
      }
    }
  }
 
类产品{
双倍?总价;
字符串?用户ID;
整数?数量;
双倍?小计;
字符串?id;
ProductData?产品;
国际地位;
日期时间?日期;
手推车({
//@需要这个。总价,
这就是我们的产品,
这个数量,,
这个日期,
这个.userId,
这个身份证,
这是小计,
这个,身份,,
});
CartProduct.FromDoucMont(文档快照文档){
试一试{
日期=document.data()!['date'].cast();
id=document.data()!['id'].toString();
products=ProductData.fromMap(
document.data()!['products'];//这就是我的问题所在
数量=document.data()!['quantity'].cast();
小计=document.data()!['subtotal'].cast();
userId=document.data()!['userId'].toString();
状态=document.data()!['status'].cast();
}捕获(e){
如果(e是平台异常){
打印(“错误”);
}
}
}
卡特服务公司

  Future<List<CartProduct>> getCartProducts(String userId) async {
    var query;
    try {

      //QuerySnapshot query;
       query = await FirebaseFirestore.instance.collection('users').doc(userId).collection('cart').where('status', isEqualTo: 1).get();
     // if (query.docs.isNotEmpty)
        return query.docs.map((doc) => CartProduct.fromDoucment(doc)).toList();

   
    } catch (e) {
      print('error machine');
      return query.docs.map((doc) => CartProduct.fromDoucment(doc)).toList();
    }
  }

未来getCartProducts(字符串用户ID)异步{ var查询; 试一试{ //查询快照查询; query=wait FirebaseFirestore.instance.collection('users').doc(userId).collection('cart').where('status',isEqualTo:1.get(); //if(query.docs.isNotEmpty) 返回query.docs.map((doc)=>CartProduct.fromDoucment(doc)).toList(); }捕获(e){ 打印(“错误机器”); 返回query.docs.map((doc)=>CartProduct.fromDoucment(doc)).toList(); } } 我收到此错误:类型“List”不是类型“FutureOr”的子类型

数据库结构

ProductData.dart

class ProductData {
  String? category;
  String? description;
  String? id;
  String? images;
  String? name;
  double? price;

  ProductData({
    this.category,
    this.description,
    this.id,
    this.images,
    this.name,
    this.price,
  });

  ProductData.fromDocument(DocumentSnapshot documentSnapshot) {
    id = documentSnapshot.data()!['id'];
    category = documentSnapshot.data()!['category'];
    description = documentSnapshot.data()!['description'];
    images = documentSnapshot.data()!['images'];
    name = documentSnapshot.data()!['name'];
    price = documentSnapshot.data()!['price'] + .0;
  }

  Map<String, dynamic> toMap() => {
        "id": id,
        "category": category,
        "description": description,
        "images": images,
        "name": name,
        "price": price,
      };
  Map<String, dynamic> toResumeMap() {
    return {
      "id": id,
      "category": category,
      "description": description,
      "images": images,
      "name": name,
      "price": price,
    };
  }

  factory ProductData.fromMap(Map<String, dynamic> json) => ProductData(
        id: json["id"].toString(),
        category: json["category"].toString(),
        description: json["description"].toString(),
        images: json["images"].toString(),
        name: json["name"].toString(),
        price: json["price"].toDouble() + 0.0,
      );
}

类ProductData{
字符串?类别;
字符串描述;
字符串?id;
字符串图像;
字符串?名称;
双倍价格;
产品数据({
这一类,,
这个.说明,,
这个身份证,
这是一张照片,
这个名字,
这个价格,,
});
ProductData.fromDocument(DocumentSnapshot DocumentSnapshot){
id=documentSnapshot.data()!['id'];
category=documentSnapshot.data()!['category'];
description=documentSnapshot.data()!['description'];
images=documentSnapshot.data()!['images'];
name=documentSnapshot.data()!['name'];
price=documentSnapshot.data()!['price']+.0;
}
映射toMap()=>{
“id”:id,
“类别”:类别,
“描述”:描述,
“图像”:图像,
“姓名”:姓名,
“价格”:价格,
};
映射到消费地图(){
返回{
“id”:id,
“类别”:类别,
“描述”:描述,
“图像”:图像,
“姓名”:姓名,
“价格”:价格,
};
}
factory ProductData.fromMap(Map json)=>ProductData(
id:json[“id”].toString(),
category:json[“category”].toString(),
description:json[“description”].toString(),
images:json[“images”].toString(),
名称:json[“name”].toString(),
price:json[“price”].toDouble()+0.0,
);
}

如果使用
try-catch
,则必须同时返回
try
catch
块中的值。

如果使用
try-catch
,则必须同时返回
try
catch
块中的值。

我设法找到了问题,我必须将数据转换为我使用的方法类型从下面接收是解决方案

 Future<List<CartProduct>> getCartProducts(String userId) async {
    var query;
    try {
      //QuerySnapshot query;
      query = await FirebaseFirestore.instance
          .collection('users')
          .doc(userId)
          .collection('cart')
          .where('status', isEqualTo: 1)
          .get();
      // if (query.docs.isNotEmpty)
      return query.docs
          .map<CartProduct>((doc) => CartProduct.fromDocument(doc))
          .toList();
    } catch (err) {
      print('Caught error: $err');
      return query.docs
          .map<CartProduct>((doc) => CartProduct.fromDocument(doc))
          .toList();
    }
  }
未来getCartProducts(字符串用户ID)异步{ var查询; 试一试{ //查询快照查询; query=等待FirebaseFirestore.instance .collection('用户') .doc(用户ID) .collection('购物车') .其中('状态',isEqualTo:1) .get(); //if(query.docs.isNotEmpty) 返回query.docs .map((doc)=>CartProduct.fromDocument(doc)) .toList(); }捕捉(错误){ 打印(“捕获错误:$err”); 返回query.docs .map((doc)=>CartProduct.fromDocument(doc)) .toList(); } }
我设法找到了问题所在,我必须将数据转换为我从下面收到的方法类型,这就是解决方案

 Future<List<CartProduct>> getCartProducts(String userId) async {
    var query;
    try {
      //QuerySnapshot query;
      query = await FirebaseFirestore.instance
          .collection('users')
          .doc(userId)
          .collection('cart')
          .where('status', isEqualTo: 1)
          .get();
      // if (query.docs.isNotEmpty)
      return query.docs
          .map<CartProduct>((doc) => CartProduct.fromDocument(doc))
          .toList();
    } catch (err) {
      print('Caught error: $err');
      return query.docs
          .map<CartProduct>((doc) => CartProduct.fromDocument(doc))
          .toList();
    }
  }
未来getCartProducts(字符串用户ID)异步{ var查询; 试一试{ //查询快照查询; query=等待FirebaseFirestore.instance .collection('用户') .doc(用户ID) .collection('购物车') .其中('状态',isEqualTo:1) .get(); //if(query.docs.isNotEmpty) 返回query.docs .map((doc)=>CartProduct.fromDocument(doc)) .toList(); }捕捉(错误){ 打印(“捕获错误:$err”); 返回query.docs .map((doc)=>CartProduct.fromDocument(doc)) .toList(); } }
使用库转换类,这样您就可以从firebase获取数据。您在代码中评论问题在这一行:
products=ProductData.fromMap(document.data()!['products']);
,这样您就可以共享
ProductData.fromMap()
code?另外,查看您的Firestore结构也会很有帮助。我看到您共享了Firestore结构,但没有共享
ProductData
类,您也可以共享吗?@RafaelLemos感谢您对我现在所做的关注。提前感谢使用库转换类,这样您就可以从您在co中评论的firebase中获取数据确定问题在这一行:
products=ProductData.fromMap(document.data()!['products']);
,那么您可以共享
ProductData.fromMap()吗
code?另外,查看您的Firestore结构也会很有帮助。我看到您共享了Firestore结构,但没有共享
ProductData
类,您也可以共享吗?@RafaelLemos感谢您对我的关注。提前感谢我理解