Created
December 22, 2019 03:54
-
-
Save danahartweg/e0890532146d2e00172b6076eac2fae8 to your computer and use it in GitHub Desktop.
Listing plant varieties - Efficiently managing large Cloud Firestore lists
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:flutter_bloc/flutter_bloc.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:grow_smart/blocs/homestead/bloc.dart'; | |
import 'package:grow_smart/services/service_locator.dart'; | |
class ListPlantVarieties extends StatelessWidget { | |
@override | |
build(context) { | |
final homesteadBloc = BlocProvider.of<HomesteadBloc>(context); | |
final stream = locator<Firestore>() | |
.collection('indices') | |
.where('indexName', isEqualTo: 'plant-varieties') | |
.where('parentId', whereIn: ['root', homesteadBloc.state]).snapshots(); | |
return Scaffold( | |
body: StreamBuilder<QuerySnapshot>( | |
stream: stream, | |
builder: (_, snapshot) { | |
if (!snapshot.hasData) { | |
return const CircularProgressIndicator(); | |
} | |
return ListView( | |
children: snapshot.data.documents.fold( | |
[], | |
(acc, document) { | |
acc.addAll( | |
document.data.keys.map((key) => ListTile(title: Text(key))), | |
); | |
return acc; | |
}, | |
)); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment