以前有没有人用过Streambuilder和Flutter和GoRouter来持久化用户状态?
目前,我正在使用默认的Flutter导航,它工作正常(见下文),但我现在正尝试用GoRouter替换
我以前使用默认Flutter导航的导航设置
home: StreamBuilder(
stream: AuthMethods().authChanges,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasData) {
return const TabsScreen();
}
return const AuthScreen();
}),
);
}
我目前的goRouter代码。我想知道我可以在哪里使用Streambuilder在下面的d代码为我的应用程序持久化用户状态(或者是否Streambuilder可以在所有与go路由器一起使用)
GoRouter routeConstruct() {
return GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return const PublicScreen();
},
routes: <RouteBase>[
GoRoute(
path: Events_Screen,
builder: (BuildContext context, GoRouterState state) {
return const EventsScreen();
},
),
GoRoute(
path: "$EventDetail_Screen/:eventId",
builder: (BuildContext context, GoRouterState state) {
return EventDetailScreen(eventId: state.params['eventId']!);
},
),
...
]),
],
);
我可以使用Streambuilder与Flutter和GoRouter来保持用户身份验证状态吗?如果可以,在goRouter代码中的什么地方我可以使用Streambuilder来根据身份验证状态确定要显示哪个小部件?谢谢
###########################
类身份验证方法
class AuthMethods {
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
Stream<User?> get authChanges => _auth.authStateChanges();
// --> use of bang ! operator here
User get user => _auth.currentUser!;
Future<bool> signInWithGoogle(BuildContext context) async {
bool res = false;
try {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication? googleAuth =
await googleUser?.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken, idToken: googleAuth?.idToken);
UserCredential userCredential =
await _auth.signInWithCredential(credential);
User? user = userCredential.user;
if (user != null) {
if (userCredential.additionalUserInfo!.isNewUser) {
await _firestore.collection('users').doc(user.uid).set({
'username': user.displayName,
'uid': user.uid,
'profilePhoto': user.photoURL,
});
}
res = true;
}
} on FirebaseAuthException catch (e) {
showSnackBar(context, e.message!);
res = false;
}
return res;
}
类路由通告程序
class RouterNotifier extends ChangeNotifier {
RouterNotifier(this._authMethods) {
_authMethods.authChanges.listen((event) {
notifyListeners();
});
}
final AuthMethods _authMethods;
String? redirect(BuildContext context, GoRouterState state) {
final onLoginPage = state.location == '/$AuthScreen';
final onHomePage = state.location == '/$Tabs_Screen';
// --> The getter 'isEmpty' isn't defined for the type 'User'.
// --> User get user => _auth.currentUser!; used a bang operator
if (_authMethods.user.isEmpty && onHomePage) {
return '/$AuthScreen';
}
// --> The getter 'isEmpty' isn't defined for the type 'User'.
if (_authMethods.currentUser.isNotEmpty && onLoginPage) {
return '/$Tabs_Screen';
}
return null;
}
}
router.dart
GoRouter routeConstruct() {
return GoRouter(
refreshListenable: router,
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return const PublicScreen();
},
routes: <RouteBase>[
GoRoute(
path: Events_Screen,
builder: (BuildContext context, GoRouterState state) {
return const EventsScreen();
},
),
GoRoute(
path: "$EventDetail_Screen/:eventId",
builder: (BuildContext context, GoRouterState state) {
return EventDetailScreen(eventId: state.params['eventId']!);
},
),
]),
],
);
}
1条答案
按热度按时间a64a0gku1#
在使用路由器5.0.0之前:
Go路由器有一个名为
GoRouterRefreshStream
的类,因此在Go路由器中但是在使用路由器5.0.0之后,它们remove it
因此您必须创建自己通告程序:
移动路由器:
那么路由器如何能够像您以前使用流构建器一样工作呢?
重定向非常有用:
在
RouterNotifier
类中:}
类用户模型:
数据流: