booking_calendar
booking_calendar copied to clipboard
firebase connection
Hi, for the "model we store in the Firebase" I need to put it in the SportBooking.dart , right?
Yes, so you have a schema in your Firebase, how your data looks like. This need to be the same (just in Dart) for your model, which in the example, it's the SportBooking.dart
Yes, so you have a schema in your Firebase, how your data looks like. This need to be the same (just in Dart) for your model, which in the example, it's the SportBooking.dart
I see.
Next question:
What package is this?
How to add this package?
Yes, so you have a schema in your Firebase, how your data looks like. This need to be the same (just in Dart) for your model, which in the example, it's the SportBooking.dart
I see.
Next question: What package is this? How to add this package?
that is not package run this line it will generate g.dart file flutter pub run build_runner build --delete-conflicting-outputs
Yes, so you have a schema in your Firebase, how your data looks like. This need to be the same (just in Dart) for your model, which in the example, it's the SportBooking.dart
I see.
Next question: What package is this? How to add this package?
that is not package run this line it will generate g.dart file flutter pub run build_runner build --delete-conflicting-outputs
No longer error on g.dart because it got generated. But the import package still on error, how to fix?
Yes, so you have a schema in your Firebase, how your data looks like. This need to be the same (just in Dart) for your model, which in the example, it's the SportBooking.dart
I see.
Next question: What package is this? How to add this package?
that is not package run this line it will generate g.dart file flutter pub run build_runner build --delete-conflicting-outputs
No longer error on g.dart because it got generated. But the import package still on error, how to fix?
i think that is your case booking_util.dart import 'package:intl/intl.dart';
class BookingUtil { BookingUtil._();
static bool isOverLapping(DateTime firstStart, DateTime firstEnd, DateTime secondStart, DateTime secondEnd) { return getLatestDateTime(firstStart, secondStart) .isBefore(getEarliestDateTime(firstEnd, secondEnd)); }
static DateTime getLatestDateTime(DateTime first, DateTime second) { return first.isAfterOrEq(second) ? first : second; }
static DateTime getEarliestDateTime(DateTime first, DateTime second) { return first.isBeforeOrEq(second) ? first : second; }
static String formatDateTime(DateTime dt) { return DateFormat.Hm().format(dt); } }
extension DateTimeExt on DateTime { bool isBeforeOrEq(DateTime second) { return isBefore(second) || isAtSameMomentAs(second); }
bool isAfterOrEq(DateTime second) { return isAfter(second) || isAtSameMomentAs(second); }
DateTime get startOfDay => DateTime(year, month, day, 0, 0); DateTime get endOfDay => DateTime(year, month, day + 1, 0, 0); DateTime startOfDayService(DateTime service) => DateTime(year, month, day, service.hour, service.minute); DateTime endOfDayService(DateTime service) => DateTime(year, month, day + 1, service.hour, service.minute); }
what can i write here show error
Yes, so you have a schema in your Firebase, how your data looks like. This need to be the same (just in Dart) for your model, which in the example, it's the SportBooking.dart
I see.
Next question: What package is this? How to add this package?
that is not package run this line it will generate g.dart file flutter pub run build_runner build --delete-conflicting-outputs
No longer error on g.dart because it got generated. But the import package still on error, how to fix?
i think that is your case booking_util.dart import 'package:intl/intl.dart';
class BookingUtil { BookingUtil._();
static bool isOverLapping(DateTime firstStart, DateTime firstEnd, DateTime secondStart, DateTime secondEnd) { return getLatestDateTime(firstStart, secondStart) .isBefore(getEarliestDateTime(firstEnd, secondEnd)); }
static DateTime getLatestDateTime(DateTime first, DateTime second) { return first.isAfterOrEq(second) ? first : second; }
static DateTime getEarliestDateTime(DateTime first, DateTime second) { return first.isBeforeOrEq(second) ? first : second; }
static String formatDateTime(DateTime dt) { return DateFormat.Hm().format(dt); } }
extension DateTimeExt on DateTime { bool isBeforeOrEq(DateTime second) { return isBefore(second) || isAtSameMomentAs(second); }
bool isAfterOrEq(DateTime second) { return isAfter(second) || isAtSameMomentAs(second); }
DateTime get startOfDay => DateTime(year, month, day, 0, 0); DateTime get endOfDay => DateTime(year, month, day + 1, 0, 0); DateTime startOfDayService(DateTime service) => DateTime(year, month, day, service.hour, service.minute); DateTime endOfDayService(DateTime service) => DateTime(year, month, day + 1, service.hour, service.minute); }
I renamed the file in util folder to BookingUtil.dart. As for the import package, I directed it to BookingUtil.dart.
But AppUtil still error for me
@johnkassi and @helloooo
These are only helper methods, so our business_logic will be cleaner, but here are the codesnippets:
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'sport_booking.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
SportBooking _$SportBookingFromJson(Map<String, dynamic> json) => SportBooking(
email: json['email'] as String?,
phoneNumber: json['phoneNumber'] as String?,
placeAddress: json['placeAddress'] as String?,
bookingStart:
AppUtil.timeStampToDateTime(json['bookingStart'] as Timestamp),
bookingEnd: AppUtil.timeStampToDateTime(json['bookingEnd'] as Timestamp),
placeId: json['placeId'] as String?,
userId: json['userId'] as String?,
userName: json['userName'] as String?,
serviceName: json['serviceName'] as String?,
serviceDuration: json['serviceDuration'] as int?,
servicePrice: json['servicePrice'] as int?,
);
Map<String, dynamic> _$SportBookingToJson(SportBooking instance) =>
<String, dynamic>{
'userId': instance.userId,
'userName': instance.userName,
'placeId': instance.placeId,
'serviceName': instance.serviceName,
'serviceDuration': instance.serviceDuration,
'servicePrice': instance.servicePrice,
'bookingStart': AppUtil.dateTimeToTimeStamp(instance.bookingStart),
'bookingEnd': AppUtil.dateTimeToTimeStamp(instance.bookingEnd),
'email': instance.email,
'phoneNumber': instance.phoneNumber,
'placeAddress': instance.placeAddress,
};
AppUtil:
class AppUtil {
AppUtil._();
....
static DateTime timeStampToDateTime(Timestamp timestamp) {
return DateTime.parse(timestamp.toDate().toString());
}
static Timestamp dateTimeToTimeStamp(DateTime? dateTime) {
return Timestamp.fromDate(dateTime ?? DateTime.now()); //To TimeStamp
}
...
}
The ApiRepository part is just this, behind a repository class but no really magic:
CollectionReference bookings = FirebaseFirestore.instance.collection('bookings');
bookings.doc(placeId).collection('bookings').withConverter<SportBooking>(
fromFirestore: (snapshots, _) => SportBooking.fromJson(snapshots.data()!),
toFirestore: (snapshots, _) => snapshots.toJson(),
).where('bookingStart', isGreaterThanOrEqualTo: startOfDay)
.where('bookingStart', isLessThanOrEqualTo: endOfDay)
.snapshots(),