Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snippets/install-flutterfire/ #881

Open
utterances-bot opened this issue Aug 5, 2022 · 5 comments
Open

snippets/install-flutterfire/ #881

utterances-bot opened this issue Aug 5, 2022 · 5 comments

Comments

@utterances-bot
Copy link

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/

Copy link

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

Copy link

xdega commented Aug 28, 2022

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
Copy link

xdega commented Aug 28, 2022

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

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@xdega @utterances-bot @Haltarys @ClaireYurev and others