Skip to content

Instantly share code, notes, and snippets.

@happyharis
Created March 23, 2020 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save happyharis/f7bfc9417b008be0af0057515d885621 to your computer and use it in GitHub Desktop.
Save happyharis/f7bfc9417b008be0af0057515d885621 to your computer and use it in GitHub Desktop.
Freezed package implement in a project with firestore
// add
performLinksCrud(linksCollection, Add(newLink));
// edit
performLinksCrud(linksCollection, Update(newLink),documentID: data.documentID);
// delete
performLinksCrud(linksCollection, Delete(), documentID: data.documentID);
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
import 'package:links_landing_page/models/links.dart';
part 'user_freeze.freezed.dart';
@freezed
abstract class User implements _$User {
const User._();
factory User({String name, String id, String profilePicture}) = _User;
static User empty() {
return User(id: '', name: '', profilePicture: '');
}
static User fromDocument(DocumentSnapshot document) {
if (document == null || document.data == null) return null;
return User(
name: document.data['name'],
id: document.documentID,
profilePicture: document.data['profile_picture']);
}
}
@freezed
abstract class LinksOperation with _$LinksOperation {
const factory LinksOperation.add(Link link) = Add;
const factory LinksOperation.update(Link link) = Update;
const factory LinksOperation.delete() = Delete;
}
void performLinksCrud(
CollectionReference linksCollection,
LinksOperation operation, {
String documentID,
}) {
operation.maybeWhen(
add: (link) => linksCollection.add(link.toMap()),
delete: () => linksCollection.document(documentID).delete(),
update: (link) {
linksCollection.document(documentID).updateData(link.toMap());
},
orElse: () => print('No such operation'));
}
@rignaneseleo
Copy link

This code should be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment