typesafe_firebase_firestore 0.0.3 copy "typesafe_firebase_firestore: ^0.0.3" to clipboard
typesafe_firebase_firestore: ^0.0.3 copied to clipboard

A flutter library providing a type safe wrapper on firebase.

example/README.md

Base #

@Model()
class UserProfile extends BaseModel {
  String uid = "";
  String name = "";
}

@FirestoreService("UserDataStore")
const userDataSchema = {
  'UserProfiles': (
    type: UserProfile,
    subCollections: {'AuditTrail': (type: AuditTrailEntry)},
  ),
};

Add Document #

Future<void> addUser(String uid, String name) async {
  final UserDataStore store = UserDataStore();
  final UserProfile newUser = UserProfile();
  newUser.uid = uid;
  newUser.name = name;
  

  // Add document with autogenerated id
  final doc = (await store.UserProfiles.add(newUser));

  final AuditTrailEntry entry = AuditTrailEntry();
  entry.uid = newUser.uid;
  entry.name = newUser.name;
  entry.modifiedAt = DateTime.now();
  entry.modifiedBy = "System";

  // Add document with specified id
  final entryDoc = await doc.AuditTrail.add(entry, "1");
}

Get Document #

Future<UserProfile> getUser(String docId) async {
  final UserDataStore store = UserDataStore();
  UserProfile user = await store.UserProfiles[docId].data;
  AuditTrailEntry entry = await store.UserProfiles[docId].AuditTrail["1"].data;

  return user;
}

Get all Documents from collection #

Future<String> getAuditTrail(String docId) async {
  final UserDataStore store = UserDataStore();
  final userDoc = store.UserProfiles[docId];
  UserProfile user = await userDoc.data;

  String trail = "";
  await for (var log in userDoc.AuditTrail.getAll()) {
    trail += "${user.name} - ${log.id}: ${(await log.data).modifiedAt}\n";
  }

  return trail;
}

Listen to doc changes #

Stream<UserProfile>? _changeStream;

void _listenChanges(String docId) {
  setState(() {
    _changeStream = _store.UserProfiles[docId].changeEvents;
  });
}

StreamBuilder(
  stream: _changeStream,
  builder: (context, snapshot) {
    if (_changeStream == null) {
      return Text("Tap button to load data");
    }
    if (snapshot.connectionState == ConnectionState.waiting) {
      return CircularProgressIndicator();
    }
    if (snapshot.hasError) return Text("Error: ${snapshot.error}");

    _names += "${snapshot.data!.name}\n";

    return Text(_names);
  },
)
1
likes
160
points
132
downloads

Publisher

unverified uploader

Weekly Downloads

A flutter library providing a type safe wrapper on firebase.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

cloud_firestore, flutter, meta, typesafe_firebase_core

More

Packages that depend on typesafe_firebase_firestore