fireship.io
fireship.io copied to clipboard
snippets/install-flutterfire/
Flutter Firebase App Setup for Power Users
How to setup a new Flutter project with Firebase, Firestore, Crashlytics, Analytics, and more.
You're definitely familiar with the best coding language Dart that developers use to develop their projects and they get all their queries like "flutter launcher icon" answered properly. Developers are finding an appropriate answer about flutter launcher icon related to the Dart coding language. By visiting this online portal developers get answers concerning Dart codes question like flutter launcher icon. https://codeprozone.com/code/dart/117971/flutter-launcher-icon.html
You can wrap the whole host choosing part for the Android emulator in an assert block to remove it from the production build automatically.
// Wrap in assert with an anonymous function that returns true to exclude it from production
assert((() {
// If on Android emulator, don't use localhost
String host = Platform.isAndroid ? '10.0.2.2:8080' : 'localhost:8080';
FirebaseFirestore.instance.settings = Settings(
host: host,
sslEnabled: false,
persistenceEnabled: false,
);
return true;
})());
I don't know if I missed a step, but the "Additional Superpowers" code snippets are invalid. For example, the analytics needs to be initialized, and the "automatic traces" for performance give an issue with unused import, etc.
This snippet seems to "fix" the analytics initialization:
import 'package:firebase_analytics/firebase_analytics.dart';
FirebaseAnalytics? analytics;
void main() {
analytics = FirebaseAnalytics.instance;
runApp(const MyApp());
}
(Below applies to: Flutter 3.3.2, Google Services 4.3.3, and Firebase-Crashlytics-Gradle 2.9.0, as well as minSdkVersion 21 in your 'app-level' build.gradle)
For simplicity, I call both the Firebase Analytics and Firebase Crashlytics via their own method:
Future<void> initializeFirebaseLytics() async {
FirebaseAnalytics.instance;
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
}
The above can be successfully called only after Firebase initialization is successful (so, like once "snapshot.connectionState == ConnectionState.done" is true). If you're having issues with initializing any of the above - here's my entire main.dart:
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const App());
}
class App extends StatefulWidget {
const App({super.key});
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
final Future<FirebaseApp> _initialization = Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Text('error');
}
if (snapshot.connectionState == ConnectionState.done) {
initializeFirebaseLytics();
return MaterialApp(
routes: appRoutes,
theme: appTheme,
);
}
return const Text('loading');
},
);
}
}
Future<void> initializeFirebaseLytics() async {
FirebaseAnalytics.instance;
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
}