我有一个简单的Provider类:
import 'package:flutter/foundation.dart';
class AppState with ChangeNotifier {
bool _isLoggedIn = false;
bool get isLoggedIn => _isLoggedIn;
set isLoggedIn(bool newValue) {
_isLoggedIn = newValue;
notifyListeners();
}
}
在登录类中,如果登录成功,我会将isLoggedIn
设置为true:
void _signInWithEmailAndPassword(appState) async {
try {
final FirebaseUser user = await _auth.signInWithEmailAndPassword(
...
);
if (user != null) {
appState.isLoggedIn = true;
appState.userData = user.providerData;
...
}
} catch (e) {
setState(() {
_errorMessage = e.message;
});
}
}
按下Android上的后退按钮,用户即使在成功登录后也可以返回到此页面。所以我想知道在Widget build
之前是否可以访问Provider.of
,如果isLoggedIn
为true
,是否可以重定向用户。
现在我有了一些类似的东西:
@override
Widget build(BuildContext context) {
final appState = Provider.of<AppState>(context);
...
这只是登录视图的一个用例,但我确信此功能可以在其他情况下使用。
https://stackoverflow.com/questions/56218108
复制相似问题