masamune_model_firestore 3.1.6
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 Model Firestore
[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]
Masamune Model Firestore #
Usage #
Installation #
- Add the package to your project.
flutter pub add masamune_model_firestore
Model Definition #
- Define your models using standard Masamune annotations. Use
katana code collectionorkatana code documentto 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();
}
- Generate the code with:
katana code generate
Adapter Configuration #
- 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!