firestore_helper_methods 1.0.3 copy "firestore_helper_methods: ^1.0.3" to clipboard
firestore_helper_methods: ^1.0.3 copied to clipboard

These helpers wil help out significantly. Base Firestore methods are available. See the example for implementation.

example/example.dart

import 'dart:convert';

import 'package:firestore_helper_methods/firestore_helper_methods.dart';

void main() {}

class User extends BaseFirestoreModel {
  final String id;
  final String? email;

  User({required this.id, this.email});

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'email': email,
    };
  }

  factory User.fromMap(Map<String, dynamic> map) {
    return User(
      id: map['id'],
      email: map['email'],
    );
  }

  String toJson() => json.encode(toMap());

  factory User.fromJson(String source) => User.fromMap(json.decode(source));

  @override
  fromFirestore(Map<String, dynamic> json) => User.fromMap(json);

  @override
  Map<String, dynamic> toFirestore() => toMap();
}

class FirestoreFunctions {
  Future<User> getUserDoc({
    required User user,
    required String collectionName,
  }) async {
    return await FirestoreMethods(model: user, collection: collectionName)
        .get();
  }

  Future<List<User>> getUserCollection({
    required User user,
    required String collectionName,
  }) async {
    return await FirestoreMethods(model: user, collection: collectionName)
        .getList();
  }

  Future<void> addUser({
    required User user,
    required String collectionName,
  }) async {
    await FirestoreMethods(model: user, collection: collectionName).add();
  }

  Future<void> deleteUser({
    required User user,
    required String collectionName,
  }) async {
    await FirestoreMethods(model: user, collection: collectionName).delete();
  }

  Future<User> updateUser({
    required User user,
    required String collectionName,
  }) async {
    return await FirestoreMethods(model: user, collection: collectionName)
        .update();
  }
}
1
likes
140
points
13
downloads

Publisher

verified publishergodfrey-computing.com

Weekly Downloads

These helpers wil help out significantly. Base Firestore methods are available. See the example for implementation.

Repository (GitHub)
View/report issues

Documentation

API reference

License

GPL-3.0 (license)

Dependencies

cloud_firestore, equatable, firebase_core

More

Packages that depend on firestore_helper_methods