import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Firestore ListView Demo', home: GamesListView(), ); } } class FirestoreService { final CollectionReference gamesCollection = FirebaseFirestore.instance.collection('games'); Future> getGames() async { final QuerySnapshot snapshot = await gamesCollection.get(); return snapshot.docs; } } class GamesListView extends StatefulWidget { @override _GamesListViewState createState() => _GamesListViewState(); } class _GamesListViewState extends State { final FirestoreService _firestoreService = FirestoreService(); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Games List'), ), body: FutureBuilder>( future: _firestoreService.getGames(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } if (!snapshot.hasData) { return CircularProgressIndicator(); } return ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (BuildContext context, int index) { final doc = snapshot.data![index]; return ListTile( title: Text(doc['name']), ); }, ); }, ), ); } }