有什么方法可以在 riverpod 的提供程序中传递参数
Is there any ways to pass parameters in riverpod's provider
如何将 FirebaseAuth 用户传递给 riverpod 中的 StreamProvider?
使用旧的提供程序包时,
我曾经从 FirebaseAuth 获取 user.uid 并将变量传递给 StreamProvider,如果我们可以获得 user.uid.
则返回 StreamProvider
@override
Widget build(BuildContext context) {
final FirebaseAuthService authService =
Provider.of<FirebaseAuthService>(context, listen: false);
return StreamBuilder<User>(
stream: authService.onAuthStateChanged,
builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
final User loggedInUser = snapshot.data;
if (loggedInUser != null) {
final String userId = loggedInUser.userId;
return MultiProvider(
providers: <SingleChildWidget>[
StreamProvider<User>(
initialData: User.initialData(),
create: (_) => DB.instance.getLoggedInUserStream(
loggedInUserId: userId,
),
您可以使用 Riverpod 的 family 功能。
final user = StreamProvider.family<User, String>((ref, uid) {
// use uid to get User
return User;
});
然后使用提供商:
useProvider(user(uid));
或者,您可以 read the value 直接从您的 FirebaseAuthService 提供商那里获得用户提供商。
final user = StreamProvider<User>((ref) {
final stream = ref.watch(FirebaseAuthService.stream);
// get uid from stream data
return User;
});
使用 family 修饰符将外部值传递给提供者。
final userStreamProvider = StreamProvider.family<User, String>((ref, uid) {
return User(id: uid);
});
使用provider时,需要传值给userStreamProvider
对于 flutter_riverpod 用户,
Widget build(BuildContext context, ScopedReader watch) {
final user = watch(userStreamProvider(uid));
}
对于 hooks_riverpod 用户,
Widget build(BuildContext) {
final user = useProvider(userStreamProvider(uid));
}
如何将 FirebaseAuth 用户传递给 riverpod 中的 StreamProvider?
使用旧的提供程序包时, 我曾经从 FirebaseAuth 获取 user.uid 并将变量传递给 StreamProvider,如果我们可以获得 user.uid.
则返回 StreamProvider@override
Widget build(BuildContext context) {
final FirebaseAuthService authService =
Provider.of<FirebaseAuthService>(context, listen: false);
return StreamBuilder<User>(
stream: authService.onAuthStateChanged,
builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
final User loggedInUser = snapshot.data;
if (loggedInUser != null) {
final String userId = loggedInUser.userId;
return MultiProvider(
providers: <SingleChildWidget>[
StreamProvider<User>(
initialData: User.initialData(),
create: (_) => DB.instance.getLoggedInUserStream(
loggedInUserId: userId,
),
您可以使用 Riverpod 的 family 功能。
final user = StreamProvider.family<User, String>((ref, uid) {
// use uid to get User
return User;
});
然后使用提供商:
useProvider(user(uid));
或者,您可以 read the value 直接从您的 FirebaseAuthService 提供商那里获得用户提供商。
final user = StreamProvider<User>((ref) {
final stream = ref.watch(FirebaseAuthService.stream);
// get uid from stream data
return User;
});
使用 family 修饰符将外部值传递给提供者。
final userStreamProvider = StreamProvider.family<User, String>((ref, uid) {
return User(id: uid);
});
使用provider时,需要传值给userStreamProvider
对于 flutter_riverpod 用户,
Widget build(BuildContext context, ScopedReader watch) {
final user = watch(userStreamProvider(uid));
}
对于 hooks_riverpod 用户,
Widget build(BuildContext) {
final user = useProvider(userStreamProvider(uid));
}