|
1 | 1 | import 'package:lit_firebase_auth/lit_firebase_auth.dart'; |
2 | 2 | import 'package:flutter/material.dart'; |
| 3 | +import 'package:firebase_core/firebase_core.dart'; |
3 | 4 |
|
4 | | -void main() => runApp(MyApp()); |
| 5 | +void main() { |
| 6 | + WidgetsFlutterBinding.ensureInitialized(); // Needed for Firebase core |
| 7 | + runApp(MyApp()); |
| 8 | +} |
5 | 9 |
|
6 | 10 | class MyApp extends StatelessWidget { |
| 11 | + // Create the initilization Future outside of `build`: |
| 12 | + final Future<FirebaseApp> _initialization = Firebase.initializeApp(); |
| 13 | + |
7 | 14 | @override |
8 | 15 | Widget build(BuildContext context) { |
9 | | - // Initialize Lit Firebase Auth. Needs to be above [MaterialApp] |
10 | | - return LitAuthInit( |
11 | | - authProviders: const AuthProviders( |
12 | | - emailAndPassword: true, // enabled by default |
13 | | - google: true, |
14 | | - apple: true, |
15 | | - anonymous: true, |
16 | | - github: true, |
17 | | - twitter: true, |
18 | | - ), |
19 | | - child: MaterialApp( |
20 | | - title: 'Material App', |
21 | | - themeMode: ThemeMode.light, |
22 | | - darkTheme: ThemeData.dark(), |
23 | | - theme: ThemeData( |
24 | | - visualDensity: VisualDensity.adaptivePlatformDensity, |
25 | | - buttonTheme: ButtonThemeData( |
26 | | - buttonColor: Colors.white, |
27 | | - textTheme: ButtonTextTheme.primary, |
28 | | - height: 40, |
29 | | - ), |
30 | | - ), |
31 | | - home: SplashScreen(), |
32 | | - ), |
| 16 | + // Before Lit Auth can be used, Firebase needs to be initialized and the |
| 17 | + // initialization needs to finish. |
| 18 | + return FutureBuilder( |
| 19 | + future: _initialization, |
| 20 | + builder: (context, snapshot) { |
| 21 | + // Check for errors |
| 22 | + if (snapshot.hasError) { |
| 23 | + return Center(child: Text('Something went wrong')); |
| 24 | + } |
| 25 | + // Once complete, show your application |
| 26 | + if (snapshot.connectionState == ConnectionState.done) { |
| 27 | + // Initialize Lit Firebase Auth. Needs to be called before |
| 28 | + // `MaterialApp`, to ensure all of the child widget, even when |
| 29 | + // navigating to a new route, has access to the Lit auth methods |
| 30 | + return LitAuthInit( |
| 31 | + authProviders: const AuthProviders( |
| 32 | + emailAndPassword: true, // enabled by default |
| 33 | + google: true, |
| 34 | + apple: true, |
| 35 | + anonymous: true, |
| 36 | + github: true, |
| 37 | + twitter: true, |
| 38 | + ), |
| 39 | + child: MaterialApp( |
| 40 | + title: 'Material App', |
| 41 | + themeMode: ThemeMode.light, |
| 42 | + darkTheme: ThemeData.dark(), |
| 43 | + theme: ThemeData( |
| 44 | + visualDensity: VisualDensity.adaptivePlatformDensity, |
| 45 | + buttonTheme: ButtonThemeData( |
| 46 | + buttonColor: Colors.white, |
| 47 | + textTheme: ButtonTextTheme.primary, |
| 48 | + height: 40, |
| 49 | + ), |
| 50 | + ), |
| 51 | + home: SplashScreen(), |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // Otherwise, show something whilst waiting for initialization to complete |
| 57 | + return Center(child: CircularProgressIndicator()); |
| 58 | + }, |
33 | 59 | ); |
34 | 60 | } |
35 | 61 | } |
|
0 commit comments