import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart'; // Import for Firestore access
// Assuming you have a firebase_options.dart file configured
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized(); // Ensures platform-specific initialization
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Get a reference to the Firestore database
final firestore = FirebaseFirestore.instance;
runApp(MyApp(firestore: firestore));
}
class MyApp extends StatelessWidget {
final FirebaseFirestore firestore;
const MyApp({required this.firestore});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('FlutterFire Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// Fetch a document from the "cities" collection
final docRef = firestore.collection('cities').doc('yourCityId'); // Replace 'yourCityId' with the actual document ID
final docSnapshot = await docRef.get();
if (docSnapshot.exists) {
final data = docSnapshot.data();
debugPrint(data.toString()); // Print the entire document data
} else {
debugPrint('Document not found');
}
},
child: const Text('Get City Data'),
),
),
),
);
}
}
|