firestore_model 0.2.0 copy "firestore_model: ^0.2.0" to clipboard
firestore_model: ^0.2.0 copied to clipboard

outdated

Firestore Model easy to use firestore collections by model data

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>

Config #

void main() async {
  await FirebaseApp.initializeApp(
      settings: FirestoreModelSettings(
          //persistenceEnabled: true,
          ));
  runApp(MyApp());
}

Data Model #

  • define your model User
  • extend model with FirestoreModel with your model Type
class User extends FirestoreModel<User> {
}
  • 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);
  
}
  • we use model name as collection name in this example User
  • if you wont to change collection name override collectionName in your model
@override
String get collectionName => 'users';

Inject Model #

  • Inject your Model
FirestoreModel.inject(User());
  • Inject All your Models
FirestoreModel.injectAll([User()]);
  • retrieve your model
FirestoreModel.use<User>();

Create #

To Write in data base:

  • prepare your data model like this:
User user = User(firstName: "Mohamed", lastName: "Abdullah 3");
  • call create method from your model like this:
user.create();
  • if you have id for doc:
user.create(docId: 'hdoihvnoirejiu9345j');

Save #

  • make changes to your model and call save:
user.firstName = 'new firstname';
user.save()

Update #

  • update specific fields in your model call update:
user.update(data: {
"first_name": "Mohamed",
"last_name": "Abdullah"
})
  • update specific model in your model use update by pass docId:
FirestoreModel.use<User>().update(
  docId: 'hdoihvnoirejiu9345j',
  data: {
    "first_name": "Mohamed",
    "last_name": "Abdullah"
  })

delete #

  • delete current model call delete:
user.delete();
  • delete specific model use delete by pass docId:
FirestoreModel.use<User>().delete(docId: 'hdoihvnoirejiu9345j')

Exists #

  • check if document is exists exists by docId:
bool isExists = await FirestoreModel.use<User>().exists('hdoihvnoirejiu9345j')

Find #

  • To get document data by document id call find:
User user = await FirestoreModel.use<User>().find('hdoihvnoirejiu9345j')
  • To stream document data by document id call streamFind:
Stream<User> streamUser = FirestoreModel.use<User>().streamFind('hdoihvnoirejiu9345j')

All #

  • To get all documents call all:
List<User> users = await FirestoreModel.use<User>().all()
  • To stream all documents call streamAll:
Stream<List<User>> streamUsers = FirestoreModel.use<User>().streamAll()

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

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

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),
    );
  • 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),
    );
30
likes
0
pub points
71%
popularity

Publisher

verified publisherm-it.dev

Firestore Model easy to use firestore collections by model data

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

cloud_firestore, firebase_core, flutter

More

Packages that depend on firestore_model