masamune_model_firestore 3.1.6 copy "masamune_model_firestore: ^3.1.6" to clipboard
masamune_model_firestore: ^3.1.6 copied to clipboard

A model adapter plugin that applies and expands on `katana_model_firestore` to retrieve data from Firestore.

Masamune logo

Masamune Model Firestore

Follow on GitHub Follow on X Follow on YouTube Maintained with Melos

GitHub Sponsor


[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]


Masamune Model Firestore #

Usage #

Installation #

  1. Add the package to your project.
flutter pub add masamune_model_firestore

Model Definition #

  1. Define your models using standard Masamune annotations. Use katana code collection or katana code document to generate model templates.
// lib/models/user.dart

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:masamune/masamune.dart';

part 'user.m.dart';
part 'user.g.dart';
part 'user.freezed.dart';

@freezed
@formValue
@immutable
@CollectionModelPath('user')
class UserModel with _$UserModel {
  const factory UserModel({
    @Default('') String name,
    @Default('') String email,
    @Default(ModelTimestamp.now()) ModelTimestamp createdAt,
  }) = _UserModel;

  const UserModel._();

  factory UserModel.fromJson(Map<String, Object?> json) => _$UserModelFromJson(json);

  static const document = _$UserModelDocumentQuery();
  static const collection = _$UserModelCollectionQuery();
  static const form = _$UserModelFormQuery();
}
  1. Generate the code with:
katana code generate

Adapter Configuration #

  1. Choose an adapter that fits your caching strategy and register it in your app:

CachedFirestoreModelAdapter (With local cache):

// lib/main.dart

import 'package:masamune_model_firestore/masamune_model_firestore.dart';

final modelAdapter = CachedFirestoreModelAdapter(
  options: DefaultFirebaseOptions.currentPlatform,
);

void main() {
  runMasamuneApp(
    appRef: appRef,
    modelAdapter: modelAdapter,
    (appRef, _) => MasamuneApp(
      appRef: appRef,
      home: HomePage(),
    ),
  );
}

CachedListenableFirestoreModelAdapter (With real-time updates):

final modelAdapter = CachedListenableFirestoreModelAdapter(
  options: DefaultFirebaseOptions.currentPlatform,
  listenOnlyWhenWatching: true,  // Listen only when document/collection is watched
);

Basic Operations #

Load a Collection:

final collection = ref.app.model(UserModel.collection())..load();

// Access users
for (final doc in collection) {
  print("User: ${doc.value?.name}");
}

Load a Document:

final document = ref.app.model(UserModel.document("user_id"))..load();
print("Name: ${document.value?.name}");

Create/Update:

final collection = ref.app.model(UserModel.collection());
final newDoc = collection.create();
await newDoc.save(
  UserModel(
    name: "John Doe",
    email: "john@example.com",
  ),
);

Delete:

await document.delete();

Filtering and Pagination #

Use generated filter methods for querying:

final users = ref.app.model(
  UserModel.collection()
    .name.equal("John")
    .createdAt.greaterThan(ModelTimestamp.now().subtract(Duration(days: 7)))
    .limitTo(20),
)..load();

// Load next page
await users.next();

GitHub Sponsors #

Sponsors are always welcome. Thank you for your support!

https://github.com/sponsors/mathrunet

0
likes
150
points
3
downloads

Publisher

verified publishermathru.net

Weekly Downloads

A model adapter plugin that applies and expands on `katana_model_firestore` to retrieve data from Firestore.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, katana, katana_model_firestore, masamune, meta

More

Packages that depend on masamune_model_firestore