Building Flutter Boilerplate
It sounds like the user needs a boilerplate for a Flutter mobile app with Firebase integration. They probably want the project structure and minimal code, including elements like authentication or Firestore. I'll ensure to cover Firebase initialization and provide instructions for setting up the Firebase project. I'll make the response concise yet usable and assume the latest Flutter stable version. I should integrate core components like firebase_core, analytics, auth, and firestore. Also, mentioning the FlutterFire CLI for setup is important!Configuring iOS & Android Integration
I want to ensure the user gets a solid foundation for iOS and Android platform-specific config without providing exact configurations from the Firebase console. It's essential to avoid using emojis and include code blocks.
I'll begin by listing necessary prerequisites like the Flutter SDK, Firebase project, and FlutterFire CLI. Then, I’ll outline key CLI commands, the required components in pubspec.yaml, and the main.dart initialization steps using Firebase.initializeApp.
Lastly, I'll suggest a simple folder structure in lib/ and include an example auth_service.dart for minimal Firebase integration while keeping things concise.Creating a Simple Counter App
I'm thinking about providing a simple counter app where the counter value is stored in Firestore. This will require setting up rules for reading and writing, which is an instructive exercise. Given the need for brevity, I'll just include one read/write example.
I'll assume no internet connection but base my responses on my knowledge up to October 2024. I need to ensure I mention the potential breaking changes in version 3.x for firebase_core and provide appropriate version constraints. I might suggest users check pub.dev for the latest compatible versions, but examples will rely on versions from my last update.Clarifying Version Constraints and App Structure
I need to recall the firebase_core and cloud_firestore versions from early 2024, like firebase_core 3.1.1 and cloud_firestore 5.0.1. However, I think it's better to give version constraints to avoid issues later, although slight discrepancies won’t cause too much trouble since Flutter's pub solver will select best available versions.
For the boilerplate, I should design a MyApp stateless widget and a HomePage stateful widget, using a FutureBuilder for Firebase initialization. The structure will include files like main.dart and app.dart, but I need to keep it simple since they just want a boilerplate without going into architecture details.Developing a Flutter + Firebase Boilerplate
To create a simple Flutter app with Firebase integration, I’ll provide clear setup instructions without long explanations. We'll separate lib/main.dart and lib/src/app.dart, potentially using MaterialApp.router.
I’ll outline step-by-step instructions for project creation, Firebase setup, and necessary dependencies in pubspec.yaml. Sample code for main.dart will also include Firestore integration, specifically reading and updating a counter.
Importantly, I’ll remind users to ensure their Firestore rules allow access, especially during testing, but to set proper security for production later. This keeps everything straightforward and reusable for future projects! 1. Create the Flutter project
# New Flutter app
flutter create firebase_boilerplate
cd firebase_boilerplate
- Add Firebase to the project
2.1 Install FlutterFire CLI
dart pub global activate flutterfire_cli
Make sure ~/.pub-cache/bin is in your PATH.
2.2 Create a Firebase project
In the Firebase console:
- Create a project.
- Add Android app (and iOS app if needed).
- Download
google-services.json (Android) / GoogleService-Info.plist (iOS) OR just rely on FlutterFire CLI in the next step.
2.3 Configure FlutterFire
From your project root:
flutterfire configure
Follow the prompts to:
- Select your Firebase project.
- Select platforms (Android, iOS, maybe web).
- This generates
lib/firebase_options.dart and updates native files.
- Add dependencies
pubspec.yaml (only the relevant parts):
name: firebase_boilerplate
description: A Flutter + Firebase starter
environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
flutter:
sdk: flutter
# Firebase core (required)
firebase_core: ^3.0.0
# Common Firebase services (optional, remove what you don't use)
cloud_firestore: ^5.0.0
firebase_auth: ^5.0.0
cupertino_icons: ^1.0.6
Then:
flutter pub get
- Boilerplate app code
4.1 lib/main.dart
This initializes Firebase and shows a simple Firestore-backed counter.
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Firebase
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Firebase Boilerplate',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
// Reference to a single Firestore document
DocumentReference<Map<String, dynamic>> get _counterRef =>
FirebaseFirestore.instance.collection('counters').doc('main');
Future<void> _increment() async {
await FirebaseFirestore.instance.runTransaction((txn) async {
final snap = await txn.get(_counterRef);
final current = (snap.data()?['value'] ?? 0) as int;
txn.set(_counterRef, {'value': current + 1});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Firebase Counter'),
),
body: Center(
child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: _counterRef.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
final data = snapshot.data!.data();
final value = (data?['value'] ?? 0) as int;
return Text(
'Counter: $value',
style: Theme.of(context).textTheme.headlineMedium,
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _increment,
child: const Icon(Icons.add),
),
);
}
}
4.2 lib/firebase_options.dart
This file is generated automatically by flutterfire configure.
Do not edit it manually; just import it as shown above.
- Firestore rules (for local testing)
In Firebase Console → Firestore → Rules, for quick development you can temporarily use:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true; // Development only, NOT for production
}
}
}
Later, tighten these rules before release.
- Run the app
flutter run
The app will:
- Initialize Firebase on startup.
- Listen to a single Firestore document (
counters/main).
- Show its
value field.
- Increment it in Firestore when you tap the FAB.
If you want, I can extend this boilerplate with:
- Basic email/password auth screen using
firebase_auth.
- A simple folder structure (e.g.,
features/, services/, widgets/).
- Environment-specific Firebase configs (dev/prod).