无法从 firebase 查询 Flutter 中获取单个记录

Can't get hold of single records from firebase query Flutter

我正在查询 Firebase 实时数据库,并且在查询本身中打印 snapshot.value 打印出正确的条目值,但是当我尝试将单个记录转换为 Item 时,所有值都为空。 forEach 循环抛出错误: 类型“(dynamic) => Null”不是 'f' 类型“(dynamic, dynamic) => void”的子类型 你能看出我做错了什么吗?

await _databaseReference
          .child('Continent')
          .child('Europe')
          .child('Country')
          .child(countryDb)
          .child('Region')
          .child(regionDb)
          .child('City')
          .child(cityDb)
          .child('Catalog')
          .orderByChild('Product Category')
          .equalTo(query)
          .once()
          .then((snapshot) {
        print(
            ' local db result is $snapshot, value is ${snapshot.value}, item is ${Item.fromFirebase(snapshot.value).toMap().toString()}'); //item's values are null

        snapshot.value.forEach((childSnapshot) {
//          results.add(Item.fromFirebase(childSnapshot));
//          print('childSnapshot is : ${Item.fromFirebase(childSnapshot)}');
          print('childSnapshot is : $childSnapshot');
        });
        return;
      });

Item.fromFirebase:

static Item fromFirebase(Map<dynamic, dynamic> map) {
    return Item(
        itemId: map['Product Id'],
        brand: map['Brand'],
        itemName: map['Product Name'],
        category: map['Product Category'],
        price: map['Product Price'],
        description: map['Product Description'],
        vendor: map['Product Vendor'],
        code: map['Code'],
        isPromotion: map['isPromotion'],
        imageUrl: map['Product Picture Url']);
  }

找到问题了。 foreach 类型错误。由于值类型是 Map 仅传入一个值与类型不匹配。

传入 2 个值它确实按预期工作。

await _databaseReference
          .child('Continent')
          .child('Europe')
          .child('Country')
          .child(countryDb)
          .child('Region')
          .child(regionDb)
          .child('City')
          .child(cityDb)
          .child('Catalog')
          .orderByChild('Product Category')
          .equalTo(query)
          .once()
          .then((snapshot) {
        print(' local db result is $snapshot, value is ${snapshot.value}');

        snapshot.value.forEach((key, childSnapshot) {
          print('childSnapshot is : $childSnapshot');
          print(
              'childSnapshot is : ${Item.fromFirebase(childSnapshot).toMap().toString()}');
          results.add(Item.fromFirebase(childSnapshot));
        });
        return;
      });