build method
- BuildContext context
override
Builds the UI based on the user's authentication state and type.
Uses a StreamBuilder to monitor Firebase authentication state and a FutureBuilder to fetch the user type, directing to the appropriate screen.
Parameters:
- context: The BuildContext for building the widget.
Returns: A Widget representing the initial screen (home or welcome).
Implementation
@override
Widget build(BuildContext context) {
final authService = Provider.of<AuthService>(context);
return StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
if (snapshot.hasData && snapshot.data != null) {
return FutureBuilder<UserType?>(
future: authService.getUserType(snapshot.data!.uid),
builder: (context, typeSnapshot) {
if (typeSnapshot.connectionState == ConnectionState.waiting) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return typeSnapshot.data == UserType.farmer
? const FarmerHomeScreen()
: const ConsumerHomeScreen();
},
);
}
return const WelcomeScreen();
},
);
}