class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late Stream<fb_auth.User?> authChanges;
@override
void initState() {
authChanges = fb_auth.FirebaseAuth.instance.authStateChanges();
super.initState();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus &&
currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus!.unfocus();
}
},
child: Consumer<ThemeProvider>(builder: (context, provider, _) {
return MaterialApp(
debugShowCheckedModeBanner: kDebugMode ? true : false,
title: 'MY app',
themeMode: provider.themeMode,
home: StreamBuilder<fb_auth.User?>(
stream: authChanges,
builder: (BuildContext ctx,
AsyncSnapshot<fb_auth.User?> firebaseAuthSnapshot) {
final user = firebaseAuthSnapshot.data;
if (ConnectionState.active ==
firebaseAuthSnapshot.connectionState) {
return Consumer<RemoteConfigProvider>(
builder: (context, provider, _) {
if (user == null) {
return Login();
} else {
return BottomNavBar();
}
},
);
} else {
return Center(
child: Spinner(),
);
}
},
),
);
}),
);
}
}
This is the implementation I have and I’m struggling to understand what am I doing wrong. I have my stream declared in a StatefulWidget
and it’s been set in the init
method. However, my streamBuilder
getting called twice therefore rendering child widgets multiple times.
Any ideas will be greatly appreciated !