任何帮助都会很有用。我正在使用 StreamBuilder 将项目加载到页面,第一次打开页面时,产品会出现,但在导航到另一个页面并导航回同一页面后,什么都没有出现。这是我的代码: UI 屏幕:
Widget _buildInventoryPage() {
return StreamBuilder<List<Inventory>>(
stream: _inventoryStream,
builder: (BuildContext context, AsyncSnapshot<List<Inventory>> snapshot) {
if (snapshot.hasError) return Message();
if (snapshot.connectionState == ConnectionState.waiting)
return Loading();
if (!_onSortInventory) {
_filteredInventories = [];
String searchQuery =
_inventoryQueryController.text.trim().toLowerCase();
for (Inventory inventory in snapshot.data)
if (_filterInventory(inventory, searchQuery))
_filteredInventories.add(inventory);
_inventoryTableData = DataTableDataSource(
currentRow: _inventoryRow,
data: _filteredInventories,
);
}
_onSortInventory = false;
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_inventorySearchBar(),
if (getIt<Data>().isAdmin()) _inventorySummary(),
_inventoryTable(),
],
),
);
},
);
}
数据:
class InventoryData {
Uuid uuid = Uuid();
CollectionReference collection([String companyId]) => Firestore.instance
.collection('company/${companyId ?? getIt<Data>().cid}/inventory');
QueryReference query([String companyId]) =>
collection(companyId).orderBy('name');
Future<List<Document>> queryQS([String companyId]) async =>
await query(companyId).get();
Future<List<Inventory>> queryDocuments([String companyId]) async =>
queryQS(companyId).then((value) =>
value.map((e) => Inventory.fromJson(e.map..['id'] = e.id)).toList());
Stream<List<Inventory>> snapshots([String companyId]) =>
collection(companyId).stream.map((e) =>
e.map((f) => Inventory.fromJson(f.map..['id'] = f.id)).toList());
// await queryQS().then((value) =>
// value.map((e) => Inventory.fromJson(e.map..['id'] = e.id)).toList());
DocumentReference reference(String id, [String companyId]) =>
collection(companyId).document(id);
Stream<Inventory> snapshot(String id) =>
reference(id).stream.map((e) => Inventory.fromJson(e.map..['id'] = e.id));
Future<Inventory> document(String id, [String companyId]) async =>
await reference(id, companyId)
.get()
.then((e) => Inventory.fromJson(e.map..['id'] = e.id));
Future<Inventory> inventory(String id, [String companyId]) async =>
(await document(id, companyId));
String add(Inventory inventory) {
String docId = inventory.id ?? getDocId();
reference(docId).set(inventory.toJson());
return docId;
}
void update(String id, Inventory inventory) =>
reference(id).update(inventory.toJson());
void delete(String id) async {
DocumentReference ref = reference(id);
List<Batch> batchQuery = await BatchData().queryDocuments(ref);
for (Batch doc in batchQuery) await reference(doc.id).delete();
ref.delete();
}
Future<Inventory> getBarcode(String barcode) async {
List<Document> query =
await collection().where('barcode', isEqualTo: barcode).limit(1).get();
if (query.length == 0) return null;
return Inventory.fromJson(query.first.map);
}
String getDocId() => uuid.v1();
}
欢迎所有相关答案,我们将不胜感激。