Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bedirhanssaglam/flutter-firebase-and-record

A simple Firebase Service project that uses the Records feature offered by the Dart programming language.
https://github.com/bedirhanssaglam/flutter-firebase-and-record

dart firebase firestore flutter records

Last synced: 5 days ago
JSON representation

A simple Firebase Service project that uses the Records feature offered by the Dart programming language.

Awesome Lists containing this project

README

        


Flutter Records And Simple Firebase Service


A simple [Firebase](https://firebase.flutter.dev/docs/overview/) Service project that uses the [Records](https://dart.dev/language/records) feature offered by the [Dart](https://dart.dev/) programming language.

Recent Article 0

#### Firebase Service

```dart
/// Defining a generic function type for JSON deserialization
typedef FromJson = T Function(Map json);

/// Declaring a class for Firebase service operations
@immutable
final class FirebaseService {
Future<(R?, Failure?)> fetchDocs({
required FromJson fromJson,
required CollectionEnums path,
}) async {
try {
// Fetching documents from Firestore collection
final querySnapshot = await path.collection.get();
// Returning fetched documents if any, else returning an empty list
return querySnapshot.docs.isNotEmpty
? (
List.from(querySnapshot.docs.map((snapshot) => fromJson(snapshot.data()),).toList()) as R,
null,
)
: (List.from([]) as R, null);
} on FirebaseException catch (e) {
// Handling Firebase exceptions
return (null, Failure(e.message ?? ProductConstants.errorMessage));
} catch (e) {
// Handling other exceptions
return (null, Failure(e.toString()));
}
}
}
```

#### ViewModelMixin

```dart
import 'dart:developer' show log;

import 'package:flutter/foundation.dart' show ValueSetter;

/// Defining a mixin for view models
base mixin ViewModelMixin {
/// Method to process response from Firebase call
void processResponse(
(T?, Failure?) response, {
required ValueSetter whenSuccess,
ValueSetter? whenError,
}) {
if (response.$1 != null) {
// Calling success callback if response contains data
whenSuccess.call(response.$1 as T);
} else if (response.$2 != null) {
// Calling error callback if response contains error
if (whenError != null) whenError.call(Failure(response.$2!.errorMessage));
}
}

/// Method to create model from response
T? tryCreateModel(
(T?, Failure?) response, {
ValueSetter? whenSuccess,
ValueSetter? whenError,
}) {
T? model;
if (response.$1 != null) {
// Creating model if response contains data
model = response.$1 as T;
if (whenSuccess != null) whenSuccess.call(model as T);
} else if (response.$2 != null) {
// Logging error message if response contains error
log(response.$2!.errorMessage!);
if (whenError != null) whenError.call(Failure(response.$2!.errorMessage));
}
return model;
}
}
```

#### HomeService

```dart
/// Defining a final class HomeService
final class HomeService {
/// Constructor for HomeService which takes an instance of FirebaseService
HomeService(FirebaseService firebaseService) : _firebaseService = firebaseService;

/// Declaring a private variable _firebaseService of type FirebaseService
final FirebaseService _firebaseService;

/// Defining a method fetchHomeList which returns a Future that may resolve to a tuple containing a List of HomeModel and a Failure object
Future<(List?, Failure?)> fetchHomeList() async {
// Calling the fetchDocs method of _firebaseService
return _firebaseService.fetchDocs>(
// Providing the fromJson function to convert fetched data to HomeModel instances
fromJson: HomeModel.fromJson,
// Providing the path to the collection from which data needs to be fetched
path: CollectionEnums.list,
);
}
}
```

#### HomeViewModel

```dart
/// Defining a view model class for the home screen
final class HomeViewModel with ViewModelMixin {
/// Constructor to initialize with HomeService
HomeViewModel(this._homeService);

/// Private instance of HomeService
final HomeService _homeService;

/// Notifier for the list of home models
ValueNotifier?> homeListNotifier = ValueNotifier?>(null);

/// Notifier for failures
ValueNotifier failureNotifier = ValueNotifier(null);

/// Notifier for loading state
ValueNotifier loadingStateNotifier = ValueNotifier(LoadingState.idle);

/// Method to fetch the home list
Future fetchHomeList() async {
// Setting loading state to busy
loadingStateNotifier.value = LoadingState.busy;

// Fetching home list and updating notifiers
homeListNotifier.value = tryCreateModel>(
await _homeService.fetchHomeList(),
whenError: (Failure failure) {
failureNotifier.value = failure;
},
);

// Setting loading state to idle after fetching
loadingStateNotifier.value = LoadingState.idle;
}
}
```

#### HomeViewMixin

```dart
/// Defining a mixin for the home view
mixin HomeViewMixin on State {
late final HomeViewModel homeViewModel;

@override
void initState() {
super.initState();
// Initializing HomeViewModel with HomeService and FirebaseService
homeViewModel = HomeViewModel(HomeService(FirebaseService()));

// Fetching home list asynchronously after the view is initialized
Future.microtask(() => homeViewModel.fetchHomeList());
}
}
```