1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-01 09:13:23 +00:00

Migrate web dashboard to null safety (#928)

* update dependencies

* Update to cloud_firestore 2.x.x

* run dart migrate

* Fix analyzer warnings from null safety migration

* Fix errors resulting from null safety migration

* Fix info level warnings

* run flutter format

* fix tests

* remove unused import, format
This commit is contained in:
John Ryan
2021-10-12 14:38:34 -07:00
committed by GitHub
parent 0061b0d70d
commit 4893a24625
20 changed files with 277 additions and 301 deletions

View File

@@ -15,9 +15,9 @@ abstract class DashboardApi {
/// Manipulates [Category] data.
abstract class CategoryApi {
Future<Category> delete(String id);
Future<Category?> delete(String id);
Future<Category> get(String id);
Future<Category?> get(String id);
Future<Category> insert(Category category);
@@ -30,9 +30,9 @@ abstract class CategoryApi {
/// Manipulates [Entry] data.
abstract class EntryApi {
Future<Entry> delete(String categoryId, String id);
Future<Entry?> delete(String categoryId, String id);
Future<Entry> get(String categoryId, String id);
Future<Entry?> get(String categoryId, String id);
Future<Entry> insert(String categoryId, Entry entry);
@@ -49,7 +49,7 @@ class Category {
String name;
@JsonKey(ignore: true)
String id;
String? id;
Category(this.name);
@@ -76,7 +76,7 @@ class Entry {
DateTime time;
@JsonKey(ignore: true)
String id;
String? id;
Entry(this.value, this.time);

View File

@@ -13,15 +13,15 @@ class FirebaseDashboardApi implements DashboardApi {
@override
final CategoryApi categories;
FirebaseDashboardApi(Firestore firestore, String userId)
FirebaseDashboardApi(FirebaseFirestore firestore, String userId)
: entries = FirebaseEntryApi(firestore, userId),
categories = FirebaseCategoryApi(firestore, userId);
}
class FirebaseEntryApi implements EntryApi {
final Firestore firestore;
final FirebaseFirestore firestore;
final String userId;
final CollectionReference _categoriesRef;
final CollectionReference<Map<String, dynamic>> _categoriesRef;
FirebaseEntryApi(this.firestore, this.userId)
: _categoriesRef = firestore.collection('users/$userId/categories');
@@ -29,10 +29,10 @@ class FirebaseEntryApi implements EntryApi {
@override
Stream<List<Entry>> subscribe(String categoryId) {
var snapshots =
_categoriesRef.document(categoryId).collection('entries').snapshots();
var result = snapshots.map((querySnapshot) {
return querySnapshot.documents.map((snapshot) {
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
_categoriesRef.doc(categoryId).collection('entries').snapshots();
var result = snapshots.map<List<Entry>>((querySnapshot) {
return querySnapshot.docs.map<Entry>((snapshot) {
return Entry.fromJson(snapshot.data())..id = snapshot.id;
}).toList();
});
@@ -41,8 +41,8 @@ class FirebaseEntryApi implements EntryApi {
@override
Future<Entry> delete(String categoryId, String id) async {
var document = _categoriesRef.document('$categoryId/entries/$id');
var entry = await get(categoryId, document.documentID);
var document = _categoriesRef.doc('$categoryId/entries/$id');
var entry = await get(categoryId, document.id);
await document.delete();
@@ -52,18 +52,18 @@ class FirebaseEntryApi implements EntryApi {
@override
Future<Entry> insert(String categoryId, Entry entry) async {
var document = await _categoriesRef
.document(categoryId)
.doc(categoryId)
.collection('entries')
.add(entry.toJson());
return await get(categoryId, document.documentID);
return await get(categoryId, document.id);
}
@override
Future<List<Entry>> list(String categoryId) async {
var entriesRef = _categoriesRef.document(categoryId).collection('entries');
var querySnapshot = await entriesRef.getDocuments();
var entries = querySnapshot.documents
.map((doc) => Entry.fromJson(doc.data)..id = doc.documentID)
var entriesRef = _categoriesRef.doc(categoryId).collection('entries');
var querySnapshot = await entriesRef.get();
var entries = querySnapshot.docs
.map((doc) => Entry.fromJson(doc.data())..id = doc.id)
.toList();
return entries;
@@ -71,24 +71,24 @@ class FirebaseEntryApi implements EntryApi {
@override
Future<Entry> update(String categoryId, String id, Entry entry) async {
var document = _categoriesRef.document('$categoryId/entries/$id');
await document.setData(entry.toJson());
var document = _categoriesRef.doc('$categoryId/entries/$id');
await document.update(entry.toJson());
var snapshot = await document.get();
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
return Entry.fromJson(snapshot.data()!)..id = snapshot.id;
}
@override
Future<Entry> get(String categoryId, String id) async {
var document = _categoriesRef.document('$categoryId/entries/$id');
var document = _categoriesRef.doc('$categoryId/entries/$id');
var snapshot = await document.get();
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
return Entry.fromJson(snapshot.data()!)..id = snapshot.id;
}
}
class FirebaseCategoryApi implements CategoryApi {
final Firestore firestore;
final FirebaseFirestore firestore;
final String userId;
final CollectionReference _categoriesRef;
final CollectionReference<Map<String, dynamic>> _categoriesRef;
FirebaseCategoryApi(this.firestore, this.userId)
: _categoriesRef = firestore.collection('users/$userId/categories');
@@ -96,9 +96,9 @@ class FirebaseCategoryApi implements CategoryApi {
@override
Stream<List<Category>> subscribe() {
var snapshots = _categoriesRef.snapshots();
var result = snapshots.map((querySnapshot) {
return querySnapshot.documents.map((snapshot) {
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
var result = snapshots.map<List<Category>>((querySnapshot) {
return querySnapshot.docs.map<Category>((snapshot) {
return Category.fromJson(snapshot.data())..id = snapshot.id;
}).toList();
});
@@ -107,8 +107,8 @@ class FirebaseCategoryApi implements CategoryApi {
@override
Future<Category> delete(String id) async {
var document = _categoriesRef.document(id);
var categories = await get(document.documentID);
var document = _categoriesRef.doc(id);
var categories = await get(document.id);
await document.delete();
@@ -117,22 +117,22 @@ class FirebaseCategoryApi implements CategoryApi {
@override
Future<Category> get(String id) async {
var document = _categoriesRef.document(id);
var document = _categoriesRef.doc(id);
var snapshot = await document.get();
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
return Category.fromJson(snapshot.data()!)..id = snapshot.id;
}
@override
Future<Category> insert(Category category) async {
var document = await _categoriesRef.add(category.toJson());
return await get(document.documentID);
return await get(document.id);
}
@override
Future<List<Category>> list() async {
var querySnapshot = await _categoriesRef.getDocuments();
var categories = querySnapshot.documents
.map((doc) => Category.fromJson(doc.data)..id = doc.documentID)
var querySnapshot = await _categoriesRef.get();
var categories = querySnapshot.docs
.map((doc) => Category.fromJson(doc.data())..id = doc.id)
.toList();
return categories;
@@ -140,9 +140,9 @@ class FirebaseCategoryApi implements CategoryApi {
@override
Future<Category> update(Category category, String id) async {
var document = _categoriesRef.document(id);
await document.setData(category.toJson());
var document = _categoriesRef.doc(id);
await document.update(category.toJson());
var snapshot = await document.get();
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
return Category.fromJson(snapshot.data()!)..id = snapshot.id;
}
}

View File

@@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:uuid/uuid.dart' as uuid;
import 'api.dart';
@@ -30,7 +31,7 @@ class MockDashboardApi implements DashboardApi {
for (var i = 0; i < 30; i++) {
var date = monthAgo.add(Duration(days: i));
var value = Random().nextInt(6) + 1;
await entries.insert(category.id, Entry(value, date));
await entries.insert(category.id!, Entry(value, date));
}
}
}
@@ -42,20 +43,20 @@ class MockCategoryApi implements CategoryApi {
StreamController<List<Category>>.broadcast();
@override
Future<Category> delete(String id) async {
Future<Category?> delete(String id) async {
var removed = _storage.remove(id);
_emit();
return removed;
}
@override
Future<Category> get(String id) async {
Future<Category?> get(String id) async {
return _storage[id];
}
@override
Future<Category> insert(Category category) async {
var id = uuid.Uuid().v4();
var id = const uuid.Uuid().v4();
var newCategory = Category(category.name)..id = id;
_storage[id] = newCategory;
_emit();
@@ -88,14 +89,14 @@ class MockEntryApi implements EntryApi {
StreamController.broadcast();
@override
Future<Entry> delete(String categoryId, String id) async {
Future<Entry?> delete(String categoryId, String id) async {
_emit(categoryId);
return _storage.remove('$categoryId-$id');
}
@override
Future<Entry> insert(String categoryId, Entry entry) async {
var id = uuid.Uuid().v4();
var id = const uuid.Uuid().v4();
var newEntry = Entry(entry.value, entry.time)..id = id;
_storage['$categoryId-$id'] = newEntry;
_emit(categoryId);
@@ -104,10 +105,12 @@ class MockEntryApi implements EntryApi {
@override
Future<List<Entry>> list(String categoryId) async {
return _storage.keys
var list = _storage.keys
.where((k) => k.startsWith(categoryId))
.map((k) => _storage[k])
.whereNotNull()
.toList();
return list;
}
@override
@@ -127,14 +130,14 @@ class MockEntryApi implements EntryApi {
void _emit(String categoryId) {
var entries = _storage.keys
.where((k) => k.startsWith(categoryId))
.map((k) => _storage[k])
.map((k) => _storage[k]!)
.toList();
_streamController.add(_EntriesEvent(categoryId, entries));
}
@override
Future<Entry> get(String categoryId, String id) async {
Future<Entry?> get(String categoryId, String id) async {
return _storage['$categoryId-$id'];
}
}