无法读取表键但在颤动中获取实时数据库中的键值
Posted
技术标签:
【中文标题】无法读取表键但在颤动中获取实时数据库中的键值【英文标题】:Unable to read table keys but getting keys values in real-time database in flutter 【发布时间】:2022-01-15 03:47:55 【问题描述】:在下面的所有代码中都描述了如何从实时数据库中获取数据并获取数据而不是数据中的父键是?
In this image I want to read Table keys and with other values but I get only values data.
这是一个模型类
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 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 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());
,
),
),
],
),
),
);
【问题讨论】:
你想在哪里读取密钥? 在主类中打印所有实时数据库的卡片视图中的键和它们的值。但我的问题是如何在 Clickable-Card 中一一获取表键及其值 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:您无法直接获取表的键,但您可以在推送整个查询之前获取键。
String key = _dbRefe.push().key
那么你必须把这个键放在查询中。
这里是数据库引用并将根表名作为 (dbTName) 传递,然后将子表名 (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);
【讨论】:
以上是关于无法读取表键但在颤动中获取实时数据库中的键值的主要内容,如果未能解决你的问题,请参考以下文章
如何根据 Angular 6 中的 Firebase 实时数据库中的键获取单个记录