Created
June 3, 2020 10:35
-
-
Save sla-000/6854b9bbbe8bfa98fb71f9e14551d94a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| Модель Project: | |
| ... | |
| const String kProjectsCollectionName = 'projects'; | |
| class ProjectModel { | |
| /// Create new project | |
| ProjectModel({ | |
| this.user, | |
| this.name, | |
| }); | |
| /// No project to show | |
| ProjectModel.empty(); | |
| /// Fill project data from document snapshot | |
| ProjectModel.fromMap(Map<String, dynamic> map, this.reference) { | |
| user = map[kField_user]; | |
| name = map[kField_name]; | |
| } | |
| static const String kField_user = 'user'; | |
| static const String kField_name = 'name'; | |
| String user; | |
| String name; | |
| DocumentReference reference; | |
| bool isEmpty() => user == null; | |
| Map<String, dynamic> toMap() { | |
| final Map<String, dynamic> map = <String, dynamic>{}; | |
| map[kField_user] = user; | |
| map[kField_name] = name; | |
| return map; | |
| } | |
| @override | |
| String toString() { | |
| return 'ProjectModel{user: $user, name: $name}'; | |
| } | |
| } | |
| ... | |
| Получить список проектов в подписке: | |
| ... | |
| _projectsSubscription = Firestore.instance | |
| .collection(kProjectsCollectionName) | |
| .where(ProjectModel.kField_user, isEqualTo: email) | |
| .snapshots() | |
| .listen((QuerySnapshot querySnapshot) { | |
| if (querySnapshot?.documents == null) { | |
| _log.warning(() => '_subscribeOnProjects: snapshot is null or empty'); | |
| clear(); | |
| return; | |
| } | |
| final List<ProjectModel> projectModels = querySnapshot.documents | |
| .map((DocumentSnapshot documentSnapshot) => ProjectModel.fromMap( | |
| documentSnapshot.data, documentSnapshot.reference)) | |
| .toList(growable: false); | |
| ... | |
| Обновить данные используя reference, так же можно и удалить: | |
| ... | |
| await projectModel.reference.updateData(<String, dynamic>{ | |
| ProjectModel.kField_name: name, | |
| }); | |
| ... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment