I'm following FlutterFire getting started - https://firebase.flutter.dev/docs/overview.
When I run the sample codes at initializing flutter fire, the FutureBuilder is invoked 3 times, the first for snapshot.connectionState is waiting, the second and the third for snapshot.connectionState is done.
I don't understand the reason why FutureBuilder is called twice with the snapshot.connectionState == ConnectionState.done
Below is my code and exactly same with the example in the FlutterFire getting started page.
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(App());
}
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (coontext, snapshot) {
if (snapshot.hasError) {
Text("snapshot error. $snapshot");
}
if (snapshot.connectionState == ConnectionState.done) {
// this is printed twice!!
print("Initialize Firebase Done!!");
return MyHome();
}
return Loading();
}
);
}
}
class Loading extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Container(
child: Text("Loading..."),
),
),
)
);
}
}
class MyHome extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State {
@override
void initState() {
FirebaseAuth
.instance
.idTokenChanges()
.listen((User? user) {
if (user == null) {
print('User is signed out');
} else {
print('User is signed in!.' + user.displayName!);
}
});
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Text('hello'),
)
);
}
}