Firebase Utils
This package offers a range of utilities for Firestore and Fire Storage, including CRUD operations and other essential functions. You can easily perform common database operations and efficiently store and manage data, allowing you to focus on developing the core functionality of your app.
Installation
To use Firebase Utils in your project, add the following dependency to your pubspec.yaml
file
dependencies:
firebase_utils: ^<latest-version>
Then run flutter pub get
to install the package.
Example
Usage
Firestore Utilities
Import the package in your Dart code
import 'package:firebase_utils/entity/firestore_doc.dart';
import 'package:firebase_utils/firebase/firestore_service.dart';
Create an Entity UserInfo which can extends FireStoreDoc or TimeStampedFireStoreDoc. If
you need createdDate and updatedDate fields in firestore table then extends with TimeStampedFireStoreDoc.
class UserInfo extends FireStoreDoc {
final String name;
final String email;
final String profileImage;
UserInfo(
{required this.name, required this.email, required this.profileImage});
@override
Map<String, dynamic> toMap() => <String, dynamic>{
'name': name,
'email': email,
'profileImage': profileImage,
};
factory UserInfo.fromMap(Map<String, dynamic> map) => UserInfo(
name: map['name'] as String,
email: map['email'] as String,
profileImage: map['profileImage'] as String,
);
}
Create a collection reference of users.
users is a collection type of UserInfo in firestore db
CollectionReference<UserInfo> get _userCollectionRef =>
widget._fireStoreService.getCollectionRef(
'users', (snapshot, _) => UserInfo.fromMap(snapshot.data()!));
Get a instance of FireStoreService
to perform all Firestore operations.
final FireStoreService firestoreService = FireStoreService.getInstance(FirebaseFirestore.instance)
Fetch all documents of collection in Streams
firestoreService.getListStream(_userCollectionRef).listen((event) {
List<UserInfo> = event
})
Insert data
firestoreService.add(UserInfo(name: 'abc',email: '[email protected]',profileImage:'imageUrl'),_userCollectionRef)
Delete Object
await fireStoreService.delete(_userCollectionRef.doc(docId));
You can use all other functions for Firestore operations defined in FireStoreService.
FirebaseStorage Utilities
Import the package in your Dart code
import 'package:firebase_utils/firebase/firebase_storage_service.dart';
Get a instance of FirebaseStorageService
to perform all FirebaseStorage operations.
final FirebaseStorageService firebaseStorageService = FirebaseStorageService.getInstance( FirebaseStorage.instance)
Upload a file
firebaseStorageService.updateFileAndGetUrl(downloadUrl, file)
Author
Connect with Us:
Contributing
Contributions, issues, and feature requests are welcome!
Show your Support
Give a star if this project helped you.
Bugs and feature requests
Have a bug or a feature request? Please first search for existing and closed issues.
If your problem or idea is not addressed yet, please open a new issue.
Copyright & License
Code copyright 2023–2024 DevCrew I/O.
Code released under the MIT license.
Comments are closed.