无法读取 table 键,但在实时数据库中获取键值

Unable to read table keys but getting keys values in real-time database in flutter

在下面的所有代码中都描述了如何从实时数据库中获取数据并获取数据而不是数据中的父键是?

In this image I want to read Table keys and with other values but I get only values data.

这是一个模型class

class Seller {
  final String address,
      businessType,
      description,
      fcm,
      name,
      onlineStatus,
      picUrl,
      uuid;
  final String blockByAdmin, completeOrders, rating;
  final String lat, lng;
  final String timeStamp;

  Seller(
      {this.address,
      this.blockByAdmin,
      this.businessType,
      this.description,
      this.completeOrders,
      this.fcm,
      this.timeStamp,
      this.lat,
      this.lng,
      this.name,
      this.onlineStatus,
      this.picUrl,
      this.rating,
      this.uuid});

  factory Seller.fromRTDB(Map<String, dynamic> snapshot) {
    return Seller(
      address: snapshot["address"],
      blockByAdmin: snapshot["blockByAdmin"],
      businessType: snapshot["businessType"],
      description: snapshot["description"],
      completeOrders: snapshot["completeOrders"],
      fcm: snapshot["fcm"],
      timeStamp: snapshot["joinTimeStamp"],
      lat: snapshot["lat"],
      lng: snapshot["lng"],
      name: snapshot["name"],
      onlineStatus: snapshot["onlineStatus"],
      picUrl: snapshot["picUrl"],
      rating: snapshot["rating"],
      uuid: snapshot["uuid"],
    );
  }
}

这是一个数据库服务class

class DatabaseService {
  final auth = FirebaseAuth.instance;
  final _database = FirebaseDatabase.instance.reference().child('kjobhee');

  Stream<List<Seller>> getSellersStream() {
    final sellerStream = _database.child('seller').onValue;
    final streamToPublish = sellerStream.map((event) {
      final sellerMap = Map<String, dynamic>.from(event.snapshot.value);
      final sellerList = sellerMap.entries.map((e) {
        return Seller.fromRTDB(Map<String, dynamic>.from(e.value));
      }).toList();
      return sellerList;
    });
    return streamToPublish;
  }
}

 **This is a CardModel Class**


   class CardModel extends ChangeNotifier {
  List<Seller> _sellerList = [];
  final _db = FirebaseDatabase.instance.reference().child('kjobhee');
  StreamSubscription<Event> _sellerStream;
  static const SELLER_PATH = 'seller';

  List<Seller> get seller => _sellerList;

  CardModel() {
    _listenToSeller();
  }

  void _listenToSeller() {
    _sellerStream = _db.child(SELLER_PATH).onValue.listen((event) {
      final allSellers = Map<String, dynamic>.from(event.snapshot.value);
      _sellerList = allSellers.values
          .map((sellerAsJson) =>
              Seller.fromRTDB(Map<String, dynamic>.from(sellerAsJson)))
          .toList();
      notifyListeners();
    });
  }

  @override
  void dispose() {
    _sellerStream.cancel();
    super.dispose();
  }
}

这是一个主要的class

   class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: kPrimaryColor,
      ),
      drawer: NavigationDrawer(),
      body: Padding(
        padding: EdgeInsets.all(8.0),
        child: Column(
          children: [
            ChangeNotifierProvider<CardModel>(
              create: (_) => CardModel(),
              child: Consumer<CardModel>(
                builder: (context, model, child) {
                  if (model.seller != null) {
                    return Expanded(
                      flex: 1,
                      child: ListView(
                        children: [
                          ...model.seller.map(
                            (seller) => GestureDetector(
                              onTap: () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (c) => AddOrder()));
                              },
                              child: Card(
                                shadowColor: kPrimaryColor,
                                elevation: 10,
                                child: Expanded(
                                  child: Padding(
                                    padding: EdgeInsets.symmetric(
                                      horizontal:
                                          getProportionateScreenWidth(20),
                                      vertical:
                                          getProportionateScreenHeight(20),
                                    ),
                                    child: Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Padding(
                                          padding: EdgeInsets.only(right: 5.0),
                                          child: Column(
                                            children: [
                                              CircleAvatar(
                                                radius: 50,
                                              ),
                                              Text(
                                                '${seller.onlineStatus}',
                                                style: customTextStyle,
                                              ),
                                            ],
                                          ),
                                        ),
                                        Flexible(
                                          child: SingleChildScrollView(
                                            padding: EdgeInsets.only(left: 5.0),
                                            scrollDirection: Axis.horizontal,
                                            child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text(
                                                    'Name: ${seller.name}',
                                                    style: customTextStyle,
                                                  ),
                                                  Text(
                                                    'Complete Orders: ${seller.completeOrders}',
                                                    style: customTextStyle,
                                                  ),
                                                  Text(
                                                    'Ratings: ${seller.rating}',
                                                    style: customTextStyle,
                                                  ),
                                                  Text(
                                                    'BusinessType: ${seller.businessType}',
                                                    style: customTextStyle,
                                                  ),
                                                  Text(
                                                    'Description: ${seller.description}',
                                                    style: customTextStyle,
                                                  ),
                                                  SizedBox(
                                                    width: 300,
                                                    child: Text(
                                                      'Address: ${seller.address}',
                                                      maxLines: 3,
                                                      style: customTextStyle,
                                                    ),
                                                  ),
                                                ]),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          )
                        ],
                      ),
                    );
                  } else {
                    return Center(child: CircularProgressIndicator());
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

您无法直接获取 table 的密钥,但您可以在推送整个查询之前获取密钥。

String key = _dbRefe.push().key

那么您必须将此键放入查询中。

这是数据库引用,将根 table 名称作为 (dbTName) 传递,然后将子 table 名称(如 (tbOrderOfBuyers))传递到 _dbRef。 如果你想制作一个自动生成的密钥,那么你只需要像 _dbRef 那样做然后 push() 方法点键,比如 itemId.

 DatabaseReference _dbRef =
    FirebaseDatabase().reference().child(dbTName)
              .child(tbOrderOfBuyers);
    
                   String itemID = _dbRef.push().key;

                            final nextOrder = <String, dynamic>{
                              'itemID': itemID,
                              'itemName': itemName,
                              'itemQuantity': itemQuantity,
                              'itemUnit': itemUnit,
                            };
                            _dbRef
                                .child(uuid)
                                .child(orderId)
                                .child(itemID)
                                .set(nextOrder);