booking_calendar icon indicating copy to clipboard operation
booking_calendar copied to clipboard

firebase connection

Open johnkassi opened this issue 2 years ago • 8 comments

Hi, for the "model we store in the Firebase" I need to put it in the SportBooking.dart , right?

johnkassi avatar Dec 14 '22 14:12 johnkassi

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

radikris avatar Dec 14 '22 15:12 radikris

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.

image Next question: What package is this? How to add this package?

johnkassi avatar Dec 14 '22 15:12 johnkassi

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.

image 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

helloooo avatar Dec 15 '22 04:12 helloooo

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. image 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

image

No longer error on g.dart because it got generated. But the import package still on error, how to fix?

johnkassi avatar Dec 15 '22 05:12 johnkassi

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. image 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

image

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); }

helloooo avatar Dec 15 '22 06:12 helloooo

Screenshot from 2022-12-15 17-40-22

what can i write here show error

helloooo avatar Dec 15 '22 06:12 helloooo

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. image 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

image 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.

image

But AppUtil still error for me

johnkassi avatar Dec 15 '22 13:12 johnkassi

@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(),

radikris avatar Jan 02 '23 08:01 radikris