Hi, I wonder if anyone out there can help. I'm using ChatGPT to help me write a booking function.
Here's the description I use for ChatGPT:
Write me a dart function that i can use in flutter flow that takes a booking start time and end time as an argument as input and returns a boolean called “booking_made”. The function will check against 3 different booking collections in firebase (bookings1, bookings2, bookings3) to check if the time is free. Any overlap in appointment time should be considered as unavailable. If the time in bookings1 is free, the function will make the booking in bookings1 and also in bookings0 and return a boolean true. If bookings1 is busy at that time, the function will check bookings2. If the time in bookings2 is free the function will make the booking in bookings2 and also in bookings0, and return a boolean true. If bookings2 is also busy, it’ll try bookings3. If the time in bookings3 is free the function will make the booking in bookings3 and also bookings0, and return true. Finally, if bookings3 is also busy, the function will return a boolean false.
And here's the code it generates:
import 'package:cloud_firestore/cloud_firestore.dart';
Future<bool> makeBookingInFirebase(DateTime startTime, DateTime endTime) async {
FirebaseFirestore firestore = FirebaseFirestore.instance;
// Check availability in bookings1
QuerySnapshot bookings1Snapshot = await firestore
.collection('bookings1')
.where('endTime', isGreaterThan: startTime)
.where('startTime', isLessThan: endTime)
.get();
if (bookings1Snapshot.docs.isEmpty) {
// Booking in bookings1 is available
await firestore.collection('bookings1').add({
'startTime': startTime,
'endTime': endTime,
});
// Make booking in bookings0
await firestore.collection('bookings0').add({
'startTime': startTime,
'endTime': endTime,
});
return true;
} else {
// Check availability in bookings2
QuerySnapshot bookings2Snapshot = await firestore
.collection('bookings2')
.where('endTime', isGreaterThan: startTime)
.where('startTime', isLessThan: endTime)
.get();
if (bookings2Snapshot.docs.isEmpty) {
// Booking in bookings2 is available
await firestore.collection('bookings2').add({
'startTime': startTime,
'endTime': endTime,
});
// Make booking in bookings0
await firestore.collection('bookings0').add({
'startTime': startTime,
'endTime': endTime,
});
return true;
} else {
// Check availability in bookings3
QuerySnapshot bookings3Snapshot = await firestore
.collection('bookings3')
.where('endTime', isGreaterThan: startTime)
.where('startTime', isLessThan: endTime)
.get();
if (bookings3Snapshot.docs.isEmpty) {
// Booking in bookings3 is available
await firestore.collection('bookings3').add({
'startTime': startTime,
'endTime': endTime,
});
// Make booking in bookings0
await firestore.collection('bookings0').add({
'startTime': startTime,
'endTime': endTime,
});
return true;
}
}
}
return false;
}
All good so far... except that after I've entered the function name (makeBookingInFirestore) and added the arguments (both datetime startTime and endTime), I click the Save Function button and I get the error "The function is empty or cannot be parsed."
And then I'm stuck - I can't see where I've gone wrong. Any ideas? Clues?
I've pasted the full code from Flutterflow custom function below:
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/auth/firebase_auth/auth_util.dart';
bool? makeBookingInFirestore(DateTime? startTime, DateTime? endTime,) {
/// MODIFY CODE ONLY BELOW THIS LINE
import 'package:cloud_firestore/cloud_firestore.dart';
Future<bool> makeBookingInFirebase(DateTime startTime, DateTime endTime) async {
FirebaseFirestore firestore = FirebaseFirestore.instance;
// Check availability in bookings1
QuerySnapshot bookings1Snapshot = await firestore
.collection('bookings1')
.where('endTime', isGreaterThan: startTime)
.where('startTime', isLessThan: endTime)
.get();
if (bookings1Snapshot.docs.isEmpty) {
// Booking in bookings1 is available
await firestore.collection('bookings1').add({
'startTime': startTime,
'endTime': endTime,
});
// Make booking in bookings0
await firestore.collection('bookings0').add({
'startTime': startTime,
'endTime': endTime,
});
return true;
} else {
// Check availability in bookings2
QuerySnapshot bookings2Snapshot = await firestore
.collection('bookings2')
.where('endTime', isGreaterThan: startTime)
.where('startTime', isLessThan: endTime)
.get();
if (bookings2Snapshot.docs.isEmpty) {
// Booking in bookings2 is available
await firestore.collection('bookings2').add({
'startTime': startTime,
'endTime': endTime,
});
// Make booking in bookings0
await firestore.collection('bookings0').add({
'startTime': startTime,
'endTime': endTime,
});
return true;
} else {
// Check availability in bookings3
QuerySnapshot bookings3Snapshot = await firestore
.collection('bookings3')
.where('endTime', isGreaterThan: startTime)
.where('startTime', isLessThan:
/// MODIFY CODE ONLY ABOVE THIS LINE
}
I'm hoping someone out there knows the answer... I'm sure its a simple issue but I just lack the knowledge to fully understand it.
Many thanks,
John