fireship.io icon indicating copy to clipboard operation
fireship.io copied to clipboard

snippets/install-flutterfire/

Open utterances-bot opened this issue 2 years ago • 5 comments

Flutter Firebase App Setup for Power Users

How to setup a new Flutter project with Firebase, Firestore, Crashlytics, Analytics, and more.

https://fireship.io/snippets/install-flutterfire/

utterances-bot avatar Aug 05 '22 13:08 utterances-bot

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

asdali8888 avatar Aug 05 '22 13:08 asdali8888

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;
  })());

Haltarys avatar Aug 17 '22 15:08 Haltarys

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.

xdega avatar Aug 28 '22 19:08 xdega

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());
}

xdega avatar Aug 28 '22 19:08 xdega

(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;
}

ClaireYurev avatar Sep 20 '22 12:09 ClaireYurev