如何检查firebase RTDB日期是否等于我通过日期选择器给出的日期



这是数据库这是我所做的订单计数的代码;IsDelivered";值分别为true和false。

**dasboard.dart.**
import 'package:easyway/future.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:intl/intl.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';

class Dashboard extends StatefulWidget {
static DateTime _date;
Dashboard({this.app});
final FirebaseApp app;
@override
_DashboardState createState() => _DashboardState();
}
class _DashboardState extends State<Dashboard> {
final _referenceData = FirebaseDatabase.instance;
final databaseRef = FirebaseDatabase.instance.reference()
.child('orders')
.orderByChild('isDelivered');
Query dataRef;
Map myList = {};
String name;
@override
void initState() {
dataRef =
FirebaseDatabase.instance.reference().child('orders').orderByChild(
'deliveryDate');
super.initState();
}
@override
Widget build(BuildContext context) {
print(dataRef);
// Widget buildorder({Map orders,int count}){
//   return Center(child: Text(orders['isDelivered'].toString(),style: TextStyle(fontSize: 30,fontWeight: FontWeight.bold),));
// }
// final ref = _referenceData.reference();
var _height = MediaQuery
.of(context)
.size
.height;
var _width = MediaQuery
.of(context)
.size
.width;
return Scaffold(
appBar: AppBar(
title: Text("DashBoard", style: TextStyle(
color: Colors.black
),),
backgroundColor: Colors.white,
),
body: ListView(
children: [
Padding(
padding: const EdgeInsets.all(29.0),
child: GestureDetector(
onTap: () {
showDatePicker(context: context,
initialDate: Dashboard._date == null
? DateTime.now()
: Dashboard._date,
firstDate: DateTime(2018),
lastDate: DateTime(2022)
).then((value) {
setState(() {
Dashboard._date = value;
});
});
},
child: Container(
height: _height * .15,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue,
boxShadow: [
BoxShadow(
color: Colors.black38,
blurRadius: 3,
spreadRadius: 3,
offset: Offset(4.0, 4.0),
)
]
),
padding: EdgeInsets.all(10),
child: FittedBox(
child: Text(
Dashboard._date == null ? DateFormat.yMMMMEEEEd().format(
DateTime.now()).toString() : DateFormat.yMMMMEEEEd()
.format(Dashboard._date)
.toString(),
style: TextStyle(fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(left: 29, right: 14),
child: Text("Production Efficiency & Order Taken", style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 23, fontFamily: 'Lato'
),),
),
Padding(
padding: const EdgeInsets.all(25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
height: _height * .3,
width: _width * .4,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.yellow,
boxShadow: [
BoxShadow(
color: Colors.black38,
blurRadius: 2,
spreadRadius: 2,
offset: Offset(4.0, 4.0),
)
],
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Total Hours", style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 26,
color: Colors.lightBlue,
fontFamily: 'Lato'),
),
SizedBox(height: 20,),
Text("25", style: TextStyle(
fontSize: 30, fontWeight: FontWeight.bold),),
Divider(
color: Colors.white,
indent: _height / 13,
endIndent: _width / 8,
thickness: 4,
),
Text("30", style: TextStyle(
fontSize: 30, fontWeight: FontWeight.bold),)
],
),
),
SizedBox(width: 25,),
Container(
height: _height * .3,
width: _width * .4,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.yellow,
boxShadow: [
BoxShadow(
color: Colors.black38,
blurRadius: 2,
spreadRadius: 2,
offset: Offset(4.0, 4.0),
)
]
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(
left: 29, right: 29, top: 20, bottom: 29),
child: Dashboard._date == null ?SampleClass(
height: _height*.35,
length: 'Delivered :',
length1: "Postponed :",
source: databaseRef.equalTo(true).once(),
source1: databaseRef.equalTo(false).once(),
):Container()
)
],
),
);
}
}
**future.dart.**
import 'package:easyway/dashboard.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
class SampleClass extends StatefulWidget {
final double height;
final String length;
final Future<DataSnapshot> source;
final String length1;
final Future<DataSnapshot> source1;
const SampleClass({Key key,this.height,this.length,this.source,this.length1,this.source1}):super(key:key);
@override
_SampleClassState createState() => _SampleClassState();
}
class _SampleClassState extends State<SampleClass> {
Dashboard date = Dashboard();
final databaseRef = FirebaseDatabase.instance.reference()
.child('orders')
.orderByChild('isDelivered');
final datarF = FirebaseDatabase.instance.reference().child('orders').orderByChild('deliveryDate');
final Future<FirebaseApp> _future = Firebase.initializeApp();
static List<Map<dynamic, dynamic>> lists = [];
static Widget future({String length,Future<DataSnapshot> source}){
return FutureBuilder(
future: source,
builder: (context,AsyncSnapshot<DataSnapshot> snapshot){
if(snapshot.hasData){
lists.clear();
Map<dynamic,dynamic> values = snapshot.data.value;
values.forEach((key, value) {
lists.add(values);
});
return Container(
child: Text(length+lists.length.toString(),style: TextStyle(fontSize: 35,fontWeight: FontWeight.bold),),
);
}else{
return CircularProgressIndicator();
}
},
);  }
@override
Widget build(BuildContext context) {
return Container(
height: widget.height,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue,
boxShadow: [
BoxShadow(
color: Colors.black38,
blurRadius: 3,
spreadRadius: 3,
offset: Offset(4.0, 4.0),
)
]
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
future(
length: widget.length,
source: widget.source,
),
future(
length: widget.length1,
source: widget.source1,
),
FutureBuilder(
future: datarF.equalTo("17/03/2021").once(),
builder: (context,AsyncSnapshot<DataSnapshot> snapshot){
if(snapshot.hasData){
lists.clear();
Map<dynamic,dynamic> values = snapshot.data.value;
values.forEach((key, value) {
if(values!=null){
lists.add(values);
}else{
lists.length = 0;
}
});
return Text(values['deliveryDate'].toString(),style: TextStyle(fontSize: 35,fontWeight: FontWeight.bold),);
}else{
return CircularProgressIndicator();
}
},
)
],
)
);
}
}

如何检查firebase RTDB日期是否等于我通过日期选择器给出的日期,并且我想进行订单计数";isDElivered";值,该值等于通过日期选择器选择的日期。

我该如何获得解决方案请帮我解决这个问题?

实时数据库不支持多个equalTo查询。因此有必要通过第二个参数过滤列表本身。尝试获取已交付订单的列表,并通过where函数进行过滤。类似这样的东西:

Widget _buildDeliveredOrdersWidget(DateTime date) {
final ref = FirebaseDatabase.instance
.reference()
.child('orders')
.orderByChild('isDelivered')
.equalTo(true)
.once()
.then((value) => filterByDate(value.value, date));
return FutureBuilder(
future: ref,
builder: (context, AsyncSnapshot<List<Map<dynamic, dynamic>>> snapshot) {
if (snapshot.hasData) {
return Text(
"Found ${snapshot.data.length} delivered orders for $date",
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
);
} else {
return CircularProgressIndicator();
}
},
);
}
List<Map<dynamic, dynamic>> filterByDate(
List<Map<dynamic, dynamic>> orders, DateTime date) =>
orders.where((element) => element['deliveryDate'] == date).toList();

最新更新