Last updated:
0 purchases
firestore model
firestore_model #
Easy way to use firestore collection with data model
Adding Firestore Model to your project #
In your project's pubspec.yaml file,
Add firestore_model latest version to your dependencies.
# pubspec.yaml
dependencies:
firestore_model: ^<latest version>
copied to clipboard
Config #
void main() async {
await FirebaseApp.initializeApp(
settings: FirestoreModelSettings(
//persistenceEnabled: true,
));
runApp(MyApp());
}
copied to clipboard
Data Model #
define your model User
extend model with FirestoreModel with your model Type
class User extends FirestoreModel<User> {
}
copied to clipboard
override toMap && responseBuilder
class User extends FirestoreModel<User> {
String? firstName;
String? lastName;
User({this.firstName, this.lastName});
// use to read
User.fromMap(Map<String, dynamic> map) {
this.firstName = map['first_name'];
this.lastName = map['last_name'];
}
// use to write
@override
Map<String, dynamic> get toMap => {
'first_name': this.firstName,
'last_name': this.lastName,
};
@override
ResponseBuilder<User> get responseBuilder => (map) => User.fromMap(map);
}
copied to clipboard
we plural model name and use it as collection name in this example collection name will be users.
if you wont to change collection name override collectionName in your model.
@override
String get collectionName => 'users';
copied to clipboard
Inject Model #
Inject your Model
FirestoreModel.inject(User());
copied to clipboard
Inject All your Models
FirestoreModel.injectAll([User()]);
copied to clipboard
retrieve your model
FirestoreModel.use<User>();
copied to clipboard
Create #
To Write in data base:
prepare your data model like this:
User user = User(firstName: "Mohamed", lastName: "Abdullah 3");
copied to clipboard
call create method from your model like this:
user.create();
copied to clipboard
return document as a model;
if you have id for doc:
user.create(docId: 'hdoihvnoirejiu9345j');
copied to clipboard
Save #
use save to create or update model.
make changes to your model and call save:
user.firstName = 'new firstname';
user.save()
copied to clipboard
Update #
update specific fields in your model call update:
user.update(data: {
"first_name": "Mohamed",
"last_name": "Abdullah"
})
copied to clipboard
update specific model use update by pass docId:
FirestoreModel.use<User>().update(
docId: 'hdoihvnoirejiu9345j',
data: {
"first_name": "Mohamed",
"last_name": "Abdullah"
})
copied to clipboard
delete #
delete current model call delete:
user.delete();
copied to clipboard
delete specific model use delete by pass docId:
FirestoreModel.use<User>().delete(docId: 'hdoihvnoirejiu9345j')
copied to clipboard
Exists #
check if document is exists call exists by docId:
bool isExists = await FirestoreModel.use<User>().exists('hdoihvnoirejiu9345j')
copied to clipboard
Find #
To get document data by document id call find:
User user = await FirestoreModel.use<User>().find('hdoihvnoirejiu9345j')
copied to clipboard
To stream document data by document id call streamFind:
Stream<User> streamUser = FirestoreModel.use<User>().streamFind('hdoihvnoirejiu9345j')
copied to clipboard
All #
To get all documents call all:
List<User> users = await FirestoreModel.use<User>().all()
copied to clipboard
To stream all documents call streamAll:
Stream<List<User>> streamUsers = FirestoreModel.use<User>().streamAll()
copied to clipboard
First #
To get first result from your collection call first:
you can build your query like where orderBy or any query buildr methods:
User firstUser = await FirestoreModel.use<User>().first(
queryBuilder: (query) => query.where('score', isGreaterThan: 100)
.orderBy('score', descending: true)
);
copied to clipboard
Get #
To get results from your collection call get:
you can build your query like where orderBy or any query buildr methods:
List<User> topUsers = await FirestoreModel.use<User>().get(
queryBuilder: (query) => query.orderBy('score', descending: true).limit(10)
);
copied to clipboard
To stream results from your collection call streamGet:
Stream<List<User>> topUsers = await FirestoreModel.use<User>().streamGet(
queryBuilder: (query) => query.orderBy('score', descending: true).limit(10)
);
copied to clipboard
Pagination #
To get results limit and load more as pagination from your collection call paginate.
you can build your query like where orderBy or any query buildr methods.
you can set perPage in your call or set in your model
List<User> topUsers = await FirestoreModel.use<User>().paginate(
perPage: 15,
queryBuilder: (query) => query.orderBy('score', descending: true),
);
copied to clipboard
To stream results from your collection call streamPaginate:
Stream<List<User>> topUsers = await FirestoreModel.use<User>().streamPaginate(
perPage: 15,
queryBuilder: (query) => query.orderBy('score', descending: true),
);
copied to clipboard
Builders #
ModelSingleBuilder: get first or find by docId.
ModelSingleBuilder<User>(
// your query to get first result
query: (q) => q.orderBy('createdAt'),
// pass document id if you need to get only this document
docId: 'iuiouurpoeuriqwe',
builder: (_, snapshot) {
// your widget
});
copied to clipboard
ModelGetBuilder: get documents with any query.
ModelGetBuilder<User>(
// your query to get results
query: (q) => q.orderBy('createdAt'),
builder: (_, snapshot) {
// your list builder
});
copied to clipboard
ModelStreamGetBuilder: stream get documents with any query.
ModelStreamGetBuilder<User>(
// your query to stream results
query: (q) => q.orderBy('createdAt'),
builder: (_, snapshot) {
// your list builder
});
copied to clipboard
ModelStreamSingleBuilder: stream first or find by docId.
ModelStreamSingleBuilder<User>(
// your query to stream first result
query: (q) => q.orderBy('createdAt'),
// pass document id if you need to stream only this document
docId: 'iuiouurpoeuriqwe',
builder: (_, snapshot) {
// your widget
});
copied to clipboard
Builder Listener #
you can listen to any builder:
* onLoading => fire when builder is loading.
* onChange => fire when your data in stream builder changed.
* onSuccess => fire when builder return data
* onError => fire if error in query
* onEmpty => fire if builder success but data is empty
onChange: () {
print("Data Change");
},
onLoading: () {
return Center(
child: Text("Loading"),
);
},
onEmpty: () => Center(
child: Text("Sorry Your List is Empty"),
),
onSuccess: (data) {
//return builder widget;
},
onError: (error) => print("Error $error"),
copied to clipboard
FieldValue #
increment: increment field value
// this increase user score by 1;
user.increment(field: 'score');
// this increase user score by 2;
user.increment(field: 'score', value: 2);
copied to clipboard
decrement: decrement field value
// this decrease user score by 1;
user.decrement(field: 'score');
// this decrease user score by 2;
user.decrement(field: 'score', value: 2);
copied to clipboard
arrayUnion: union elements to array
// append 'kotlin' to user languages
user.arrayUnion(field: 'languages', elements: ['kotlin']);
copied to clipboard
arrayRemove: remove elements from array
// remove 'kotlin' from user languages
user.arrayRemove(field: 'languages', elements: ['kotlin']);
copied to clipboard
remove: remove field from document
// remove 'languages' field from user document
user.remove(field: 'languages');
copied to clipboard
SubCollection #
SubCollection is a collection associated with a specific document.
Prepare SubCollection Model #
create Model for SubCollection and extends model with SubCollectionModel with your model Type:
class Post extends SubCollectionModel<Post> {
}
copied to clipboard
override toMap && responseBuilder
we plural model name and use it as collection name in this example collection name will be posts.
if you wont to change subCollection name override subCollectionName in your model
@override
String get subCollectionName => 'posts';
copied to clipboard
Create in SubCollection #
use parent to work with subCollection
Post post = user.subCollection<Post>();
post.title = "new test Title For post ${DateTime.now()}";
post.description = "Description Title in post Sub collection";
post.create();
copied to clipboard
Update document in SubCollection #
// use `update` to update document in subCollection
post.update(data: {'title': 'updated title'});
// or you can use `save`
post.description = 'new description';
post.save();
copied to clipboard
if you have SubCollectionModel you can work as any FirestoreModel
Builder With SubCollection #
to use builders to retrieve subCollection Documents you must be have parentModel
ModelStreamGetBuilder<Post>(
// parent model for Post SubCollectionModel
parentModel: user,
builder: (_, snapshot) {
// your list builder
}
);
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.