Home > Mobile >  Flutter on iOS: Despite calling Firebase.initializeApp() on a future builder, app is not initialized
Flutter on iOS: Despite calling Firebase.initializeApp() on a future builder, app is not initialized

Time:10-13

The title says it all. Below are the relevant classes and their respective codes.

main.dart

... //Imports
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => NotificationService())
      ],
      child: MaterialApp(
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('en', ''),
          const Locale('pt', 'BR'),
        ],
        home: MyHomePage(),
        debugShowCheckedModeBanner: false,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StartPage(),
      ),
    );
  }
}

startPage.dart

... //Imports
class StartPage extends StatelessWidget {
  Future<void> init(BuildContext context) async {
    Firebase.initializeApp(); //Firebase initialization
    await Provider.of<NotificationService>(context, listen: false).init();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: init(context),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting)
            return LoadingSpinner();
          else {
            return StreamBuilder<User?>(
              stream: AuthService().userChanges,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return LoadingSpinner();
                } else {
                  print(snapshot.hasData);
                  return snapshot.hasData ? MainScreen() : AuthPage();
                }
              },
            );
          }
        });
  }
}

pubspec.yaml

...
version: 1.0.0 1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  flutter_spinkit: ^5.1.0
  brasil_fields: ^1.1.4
  image_picker: ^0.8.4 2
  provider: ^6.0.0
  firebase_auth: ^3.1.1
  cloud_firestore: ^2.5.3
  firebase_storage: ^10.0.3
  firebase_core: ^1.7.0
  firebase_messaging: ^10.0.7
  flutter_typeahead: ^3.2.1
  rxdart: ^0.27.2
  table_calendar: ^3.0.2
  flutter_form_builder: ^6.1.0 1
  http: ^0.13.4
  flutter_localizations: 
    sdk: flutter
    version: ^0.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter
...

When I try to run the app on android emulator, even on an android device, it works normally. However, on the iOS emulator, it shows the following error:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

Even though I call Firebase.initializeApp on startPage.dart on an async method. Please help!

CodePudding user response:

Have you try this?

    Future<void> init(BuildContext context) async {
       WidgetsFlutterBinding.ensureInitialized();//Add this line
       Firebase.initializeApp(); //Firebase initialization
       await Provider.of<NotificationService>(context, listen: false).init();
  }

But according to the documentation it's in the main(){}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(App());
}
  • Related