Created
March 23, 2020 09:00
-
-
Save happyharis/f7bfc9417b008be0af0057515d885621 to your computer and use it in GitHub Desktop.
Freezed package implement in a project with firestore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code should be updated.