Android 用于datetime颤振的Firebase基本查询
我正试图编写一个程序来检查用户选择的时间是否已经存在于firebase firestore中。如果确实如此,我将导航回他们再次选择时间的页面 但到目前为止,我成功地将日期和时间发送到了firebase,而不是后者Android 用于datetime颤振的Firebase基本查询,android,firebase,flutter,dart,google-cloud-firestore,Android,Firebase,Flutter,Dart,Google Cloud Firestore,我正试图编写一个程序来检查用户选择的时间是否已经存在于firebase firestore中。如果确实如此,我将导航回他们再次选择时间的页面 但到目前为止,我成功地将日期和时间发送到了firebase,而不是后者 DateTime _eventDate; bool processing; String _time; bool conditionsStatisfied ; @override void initState() { super.initState();
DateTime _eventDate;
bool processing;
String _time;
bool conditionsStatisfied ;
@override
void initState() {
super.initState();
_eventDate = DateTime.now();
processing = false ;
}
内部showDatePicker()
按钮内部(保存):
onPressed:()异步{
如果(_eventDate!=null){
最终QuerySnapshot结果=等待FirebaseFirestore
.例如
.collection(“事件”)
.where('event_date',isEqualTo:this.\u eventDate)
.where('selected_time',isEqualTo:this._time)
.get();
最终列表文件=result.docs;
如果(document.length>0){
设置状态(){
打印(“方法匹配条件内”);
对话(背景);
});
}否则{
最终数据={
//“标题”:_title.text,
“选定的时间”:此时间,
“事件日期”:此.\u事件日期
};
如果(widget.note!=null){
等待eventDBS.updateData(widget.note.id,数据);
}否则{
等待事件dbs.create(数据);
}
Navigator.pop(上下文);
设置状态(){
处理=假;
});
}
};
如何解决此问题需要一些指导
另外,由于else语句,现在程序不会将日期写入firestore。经过大量研究,我意识到如果您以日期时间格式从日历发送数据,那么由于日期末尾的时间戳,将无法与日期匹配。因此,我将日期时间值格式化为(DD/MM/YYYY) 以下是代码的其余部分供参考:
class _AddEventPageState extends State<AddEventPage> {
String _eventDate;
bool processing;
String _time;
@override
void initState() {
super.initState();
// _eventDate = DateTime.now();
processing = false ;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Please select a date'),),
body: Column(
children: [
hourMinute30Interval(),
Text('$_time'),
ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
ListTile(
title: Text(
'$_eventDate'),
onTap: () async {
DateTime picked = await showDatePicker(context: context,
initialDate: DateTime.now(),
firstDate: DateTime(DateTime.now().year - 1),
lastDate: DateTime(DateTime.now().year + 10),);
if (picked != null) {
setState(() {
print('inside the setState of listTile');
_eventDate = DateFormat('dd/MM/yyyy').format(picked) ;
});
}
},
),
SizedBox(height: 10.0),
ListTile(
title: Center(
child: Text('Select time for appointment!', style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
),
processing
? Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Theme
.of(context)
.primaryColor,
child:MaterialButton(
child: Text('SAVE', style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
)),
onPressed: () async {
if (_eventDate != null) {
AddingEventsUsingRajeshMethod().getAvailableSlots(
_eventDate, _time).then((QuerySnapshot docs) async {
if (docs.docs.length == 1) {
showAlertDialogue(context);
}
else{
final data = {
// "title": _title.text,
'selected_time': this._time,
"event_date": _eventDate,
};
if (widget.note != null) {
await eventDBS.updateData(widget.note.id, data);
} else {
await eventDBS.create(data);
}
Navigator.pop(context);
setState(() {
processing = false;
});
}
});
}
}
),
),
),
],
),
],
),
);
}
hourMinute30Interval()只是一个小部件,它返回一个timePickerSpinner,这是一个自定义小部件
传递_eventDate和_time后运行的查询位于另一个类中,如下所示:
class AddingEventsUsingRajeshMethod {
getAvailableSlots(String _eventDate , String _time){
return FirebaseFirestore.instance
.collection('events')
.where('event_date', isEqualTo: _eventDate )
.where('selected_time', isEqualTo: _time)
.get();
}
}
你可以给它起个更漂亮的名字;)eventDBS.updateData来自firestoreHelper的一个包“选定的时间”可能是空白?@jiholee,即使在这之后没有任何变化。
class _AddEventPageState extends State<AddEventPage> {
String _eventDate;
bool processing;
String _time;
@override
void initState() {
super.initState();
// _eventDate = DateTime.now();
processing = false ;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Please select a date'),),
body: Column(
children: [
hourMinute30Interval(),
Text('$_time'),
ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
ListTile(
title: Text(
'$_eventDate'),
onTap: () async {
DateTime picked = await showDatePicker(context: context,
initialDate: DateTime.now(),
firstDate: DateTime(DateTime.now().year - 1),
lastDate: DateTime(DateTime.now().year + 10),);
if (picked != null) {
setState(() {
print('inside the setState of listTile');
_eventDate = DateFormat('dd/MM/yyyy').format(picked) ;
});
}
},
),
SizedBox(height: 10.0),
ListTile(
title: Center(
child: Text('Select time for appointment!', style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
),
processing
? Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Theme
.of(context)
.primaryColor,
child:MaterialButton(
child: Text('SAVE', style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
)),
onPressed: () async {
if (_eventDate != null) {
AddingEventsUsingRajeshMethod().getAvailableSlots(
_eventDate, _time).then((QuerySnapshot docs) async {
if (docs.docs.length == 1) {
showAlertDialogue(context);
}
else{
final data = {
// "title": _title.text,
'selected_time': this._time,
"event_date": _eventDate,
};
if (widget.note != null) {
await eventDBS.updateData(widget.note.id, data);
} else {
await eventDBS.create(data);
}
Navigator.pop(context);
setState(() {
processing = false;
});
}
});
}
}
),
),
),
],
),
],
),
);
}
showAlertDialogue(BuildContext context) {
Widget okButton = FlatButton(onPressed: (){
Timer(Duration(milliseconds: 500), () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => datePicker()),
);
});
}, child: Text(' OK! '));
AlertDialog alert = AlertDialog(
title: Text('Slot unavailable'),
content: Text('This slot is already booked please select another slot'),
actions: [
okButton,
],
);
showDialog(context: context ,
builder: (BuildContext context){
return alert ;
}
);
}
class AddingEventsUsingRajeshMethod {
getAvailableSlots(String _eventDate , String _time){
return FirebaseFirestore.instance
.collection('events')
.where('event_date', isEqualTo: _eventDate )
.where('selected_time', isEqualTo: _time)
.get();
}
}