작성
·
149
0
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Votes'),
),
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('list').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return LinearProgressIndicator();
return ListView(
padding: EdgeInsets.symmetric(horizontal: 16,vertical: 8),
children: snapshot.data!.docs.map((data) => buildListItem(context, data)).toList(),
);
},
)
);
}
Widget buildListItem(BuildContext context, DocumentSnapshot document){
Map<String, dynamic> data =
document.data() as Map<String, dynamic>;
return Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: ListTile(
title: Text(document.data['name']),
trailing: Text(document.data['votes'].toString())
),
);