'MaterialApp widget is not rebuilding even though it is wraped with a consumer widget
I am trying to navigate to two different screens dynamically by using a ternary operator in the initialRoute of my material app but I am able to authenticate but does not update the current auth screen to my home screen. Below is the code for my materialApp widget.
Consumer<Auth>(
builder: (context, authData, _) => MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Cinema',
theme: ThemeData(brightness: Brightness.dark),
initialRoute:
authData.isAuth ? HomeScreen.routName : AuthScreen.routeName,
routes: {
AuthScreen.routeName: (context) => AuthScreen(),
HomeScreen.routName: (context) => const HomeScreen(),
MovieDetailScreen.routeName: (context) => const MovieDetailScreen(),
FavoriteMovies.routName: (context) => const FavoriteMovies(),
},
),
),
Below is the code of the auth where I check whether my token is true is false That is there is a token or not.
class Auth with ChangeNotifier {
String? _token;
DateTime? _expiryDate;
String? _userId;
bool get isAuth {
return token != null;
}
String? get token {
if (_token != null &&
_expiryDate != null &&
_expiryDate!.isAfter(DateTime.now())) {
return _token;
}
return null;
}
Future<void> authenticate(
String email, String password, String urlSegment) async {
const _apiKey = '*******************************';
final url =
'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegment?key=$_apiKey';
try {
final response = await http.post(
Uri.parse(url),
body: json.encode(
{
'email': email,
'password': password,
'returnSecureToken': true,
},
),
);
final responseData = json.decode(response.body);
if (responseData['error'] != null) {
throw HttpException(responseData['error']['message']);
}
_token = responseData['idToken'];
_userId = responseData['localId'];
_expiryDate = DateTime.now().add(
Duration(
seconds: int.parse(
responseData['expiresIn'],
),
),
);
print(isAuth); // This prints true whenever I call this authenticate method
notifyListeners();
} catch (error) {
print(error);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

