這是實時 Firebase 資料

在這張圖片中,您可以看到資料,只有我想顯示資料庫中可用的專案檔案。但它顯示了所有檔案。我只想顯示可用的資料。如果您知道如何獲得一次或使用流或任何其他解決方案,請提供幫助,因為我被困在其中 3 天。
這是主頁作業的正文
class OrderMenu extends StatefulWidget {
const OrderMenu({
Key? key,
required this.dbChild,
}) : super(key: key);
final String dbChild;
@override
_OrderMenuState createState() => _OrderMenuState();
}
class _OrderMenuState extends State<OrderMenu> {
DatabaseReference db = FirebaseDatabase.instance.reference();
User? curUser = FirebaseAuth.instance.currentUser;
int? len;
List<GetData>? data;
Map<String, dynamic>? proceed;
List<GetData>? AvailData;
late List<bool>? avail = List.generate(
len!,
(index) => false,
);
@override
Widget build(BuildContext context) {
return StreamBuilder<List<GetData>>(
stream: DataStreamPublisher(widget.dbChild).getMenuStream(),
builder: (context, snapshot) {
if (snapshot.hasData) {
data = snapshot.data;
len = data!.length;
return SafeArea(
child: SingleChildScrollView(
child: Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: double.infinity,
child: DataTable2(
showBottomBorder: true,
headingRowHeight: 40,
showCheckboxColumn: true,
sortAscending: true,
columnSpacing: 5,
dataRowHeight: 60,
dividerThickness: 2,
bottomMargin: 20.0,
checkboxHorizontalMargin: 5,
columns: [
DataColumn2(
size: ColumnSize.L,
label: Center(
child: Text(
"Item Name",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
DataColumn2(
size: ColumnSize.S,
label: Center(
child: Text(
"Price",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
DataColumn2(
size: ColumnSize.L,
label: Center(
child: Text(
"Quantity",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
],
rows: List<DataRow>.generate(
data!.length,
(index) => DataRow2(
selected: avail![index],
onSelectChanged: (bool? value) {
setState(() {
avail![index] = value!;
});
},
color: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.08);
}
return null;
},
),
cells: [
DataCell(
Text(
data![index].Juice_Name,
),
),
DataCell(
Center(
child: Text(
data![index].Price,
),
),
),
DataCell(
IncDecButton(),
),
],
),
),
),
),
],
),
),
),
);
} else if (snapshot.hasError) {
return Center(
child: Text(
'Add Item in Menu.',
style: TextStyle(
fontSize: 18,
),
),
);
}
return Center(
child: const CircularProgressIndicator(),
);
},
);
}
}
**This is stream of GetDataPublisher**
class DataStreamPublisher {
final String dbChild;
DatabaseReference database = FirebaseDatabase.instance.reference();
User? curUser = FirebaseAuth.instance.currentUser;
DataStreamPublisher(this.dbChild);
Stream<List<GetData>> getDataStream() {
final dataStream = database.child(curUser!.uid).child(dbChild).onValue;
final streamToPublish = dataStream.map((event) {
final dataMap = Map<String, dynamic>.from(event.snapshot.value);
final dataList = dataMap.entries.map((e) {
return GetData.fromRTDB(Map<String, dynamic>.from(e.value));
}).toList();
return dataList;
});
return streamToPublish;
}
Stream<List<GetData>> getMenuStream() {
final dataStream = database.child(curUser!.uid).child(dbChild).onValue;
final streamToPublish = dataStream.map((event) {
final dataMap = Map<String, dynamic>.from(event.snapshot.value);
final dataList = dataMap.entries.map((e) {
**if (e.value["Available"] == true) {
print("Here I need a condition to get available data. OR any other possibility");
}**
return GetData.fromRTDB(Map<String, dynamic>.from(e.value));
}).toList();
return dataList;
});
return streamToPublish;
}
}
這是 GetData 類
class GetData {
String Juice_Name;
String Price;
bool Available;
var key;
GetData({
required this.Juice_Name,
required this.Available,
required this.Price,
required this.key,
});
factory GetData.fromRTDB(Map<String, dynamic> data) {
return GetData(
Juice_Name: data['Juice_Name'] ?? 'Enter Juice Name',
Price: data['Price'] ?? 'Enter Price',
Available: data['Available'] ?? false,
key: data['Key'] ?? 'key',
);
}
}
uj5u.com熱心網友回復:
當您使用以下參考時:
final dataStream = database.child(curUser!.uid).child(dbChild)
您確實會獲得“dbChild”節點下的所有資料。
如果您只需要獲取將“Available”屬性設定為 true 的物件,那么您應該使用如下所示的查詢:
DatabaseReference db = FirebaseDatabase.instance.reference();
var query = db.child(curUser!.uid).child('Juices').orderByChild("Available").equalTo(true);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/341185.html
標籤:火力基地 扑 镖 Firebase 实时数据库 谷歌云平台
上一篇:FIREBASEFUNCTIONS(錯誤[ERR_PACKAGE_PATH_NOT_EXPORTED]:未定義“匯出”主要內容)
