@@ -4,128 +4,79 @@ import 'package:firebase_core/firebase_core.dart';
44
55void main () async {
66 WidgetsFlutterBinding .ensureInitialized ();
7- await Firebase .initializeApp (
8- options: DefaultFirebaseOptions .currentPlatform,
9- );
10- runApp (const MyApp ());
7+ try {
8+ await Firebase .initializeApp (
9+ options: DefaultFirebaseOptions .currentPlatform,
10+ );
11+ runApp (const MyApp ());
12+ } catch (e) {
13+ runApp (MyApp (errorMessage: 'Failed to connect to Firebase: $e ' ));
14+ }
1115}
1216
1317class MyApp extends StatelessWidget {
14- const MyApp ({super .key});
18+ final String ? errorMessage;
19+
20+ const MyApp ({super .key, this .errorMessage});
1521
16- // This widget is the root of your application.
1722 @override
1823 Widget build (BuildContext context) {
1924 return MaterialApp (
20- title: 'Flutter Demo' ,
25+ title: 'Flutter Firebase Demo' ,
2126 theme: ThemeData (
22- // This is the theme of your application.
23- //
24- // TRY THIS: Try running your application with "flutter run". You'll see
25- // the application has a purple toolbar. Then, without quitting the app,
26- // try changing the seedColor in the colorScheme below to Colors.green
27- // and then invoke "hot reload" (save your changes or press the "hot
28- // reload" button in a Flutter-supported IDE, or press "r" if you used
29- // the command line to start the app).
30- //
31- // Notice that the counter didn't reset back to zero; the application
32- // state is not lost during the reload. To reset the state, use hot
33- // restart instead.
34- //
35- // This works for code too, not just values: Most code changes can be
36- // tested with just a hot reload.
37- colorScheme: ColorScheme .fromSeed (seedColor: Colors .deepPurple),
27+ colorScheme: ColorScheme .fromSeed (seedColor: Colors .orange),
3828 useMaterial3: true ,
3929 ),
40- home: const MyHomePage (title : 'Flutter Demo Home Page' ),
30+ home: MyHomePage (errorMessage : errorMessage ),
4131 );
4232 }
4333}
4434
45- class MyHomePage extends StatefulWidget {
46- const MyHomePage ({super .key, required this .title});
47-
48- // This widget is the home page of your application. It is stateful, meaning
49- // that it has a State object (defined below) that contains fields that affect
50- // how it looks.
51-
52- // This class is the configuration for the state. It holds the values (in this
53- // case the title) provided by the parent (in this case the App widget) and
54- // used by the build method of the State. Fields in a Widget subclass are
55- // always marked "final".
35+ class MyHomePage extends StatelessWidget {
36+ final String ? errorMessage;
5637
57- final String title ;
38+ const MyHomePage ({ super .key, this .errorMessage}) ;
5839
5940 @override
60- State <MyHomePage > createState () => _MyHomePageState ();
61- }
41+ Widget build (BuildContext context) {
42+ // Additional project information to display
43+ String projectInfo = '''
44+ Flutter Firebase Demo
6245
63- class _MyHomePageState extends State <MyHomePage > {
64- int _counter = 0 ;
46+ Firebase Connection Status: ${Firebase .apps .isNotEmpty ? 'Connected' : 'Not Connected' }
6547
66- void _incrementCounter () {
67- setState (() {
68- // This call to setState tells the Flutter framework that something has
69- // changed in this State, which causes it to rerun the build method below
70- // so that the display can reflect the updated values. If we changed
71- // _counter without calling setState(), then the build method would not be
72- // called again, and so nothing would appear to happen.
73- _counter++ ;
74- });
75- }
48+ Firebase Configuration: ${DefaultFirebaseOptions .currentPlatform }
49+ ''' ;
7650
77- @override
78- Widget build (BuildContext context) {
79- // This method is rerun every time setState is called, for instance as done
80- // by the _incrementCounter method above.
81- //
82- // The Flutter framework has been optimized to make rerunning build methods
83- // fast, so that you can just rebuild anything that needs updating rather
84- // than having to individually change instances of widgets.
8551 return Scaffold (
8652 appBar: AppBar (
87- // TRY THIS: Try changing the color here to a specific color (to
88- // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
89- // change color while the other colors stay the same.
9053 backgroundColor: Theme .of (context).colorScheme.inversePrimary,
91- // Here we take the value from the MyHomePage object that was created by
92- // the App.build method, and use it to set our appbar title.
93- title: Text (widget.title),
54+ title: const Text ('Flutter Demo' ),
9455 ),
9556 body: Center (
96- // Center is a layout widget. It takes a single child and positions it
97- // in the middle of the parent.
98- child: Column (
99- // Column is also a layout widget. It takes a list of children and
100- // arranges them vertically. By default, it sizes itself to fit its
101- // children horizontally, and tries to be as tall as its parent.
102- //
103- // Column has various properties to control how it sizes itself and
104- // how it positions its children. Here we use mainAxisAlignment to
105- // center the children vertically; the main axis here is the vertical
106- // axis because Columns are vertical (the cross axis would be
107- // horizontal).
108- //
109- // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
110- // action in the IDE, or press "p" in the console), to see the
111- // wireframe for each widget.
112- mainAxisAlignment: MainAxisAlignment .center,
113- children: < Widget > [
114- const Text (
115- 'You have pushed the button this many times:' ,
116- ),
117- Text (
118- '$_counter ' ,
119- style: Theme .of (context).textTheme.headlineMedium,
120- ),
121- ],
122- ),
57+ child: errorMessage != null
58+ ? Text (
59+ errorMessage! ,
60+ style: const TextStyle (color: Colors .red, fontSize: 18 ),
61+ )
62+ :
63+ // success like /media/image.png
64+ Column (
65+ mainAxisAlignment: MainAxisAlignment .center,
66+ children: < Widget > [
67+ const Text (
68+ 'Firebase Connected Successfully!' ,
69+ style: TextStyle (color: Colors .green, fontSize: 18 ),
70+ ),
71+ const SizedBox (height: 50 ),
72+ Text (
73+ projectInfo,
74+ style: const TextStyle (color: Colors .black, fontSize: 16 ),
75+ textAlign: TextAlign .center,
76+ ),
77+ ],
78+ ),
12379 ),
124- floatingActionButton: FloatingActionButton (
125- onPressed: _incrementCounter,
126- tooltip: 'Increment' ,
127- child: const Icon (Icons .add),
128- ), // This trailing comma makes auto-formatting nicer for build methods.
12980 );
13081 }
13182}
0 commit comments