Replies: 6 comments 13 replies
-
You're expected to return @riverpod
FakeDatabase fakeDatabase(FakeDatabaseRef ref) => FakeDatabase(); |
Beta Was this translation helpful? Give feedback.
-
@hibix43 Despite using It's happening because you're updating state after the provider is disposed. You can:
child: ElevatedButton(
onPressed: () async {
final sub =
ref.listenManual(sampleNotifierProvider.notifier, (_, __) {});
await sub.read().fetch();
sub.close();
},
child: const Text('Fetch'),
),
|
Beta Was this translation helpful? Give feedback.
-
I am running into this as well. This seems like a common usecase. Are there plans to fix it? I created this extension method for now: import 'package:flutter_riverpod/flutter_riverpod.dart';
extension SafeWidgetRef on WidgetRef {
Future<Tr> safeRead<Tn, Tr>(Refreshable<Tn> notifier, Future<Tr> Function(Tn) call) async {
final sub = listenManual(notifier, (_, __) {});
final res = await call(sub.read());
sub.close();
return res;
}
} |
Beta Was this translation helpful? Give feedback.
-
Unsure if this is related, but I've also ran into the same issue. It happened with this code: @riverpod
class LocalLoginWithEmailAndPassword extends _$LocalLoginWithEmailAndPassword {
@override
FutureOr<Option<PecuniaUser>> build() {
return const Option.none();
}
Future<void> localLoginWithEmailAndPassword({
required String email,
required String password,
}) async {
state = const AsyncValue.loading();
final failureOrPecuniaUserAndSession = await ref
.read(authRepoProvider)
.localLoginWithEmailAndPassword(
email: email,
password: password,
)
.run();
return failureOrPecuniaUserAndSession.fold(
(failure) => state = AsyncValue.error(failure, failure.stackTrace),
(r) => state = AsyncValue.data(Option.of(r.user)),
);
}
} Which caused a @override
Future<Option<PecuniaUser>> build() async {
return const Option.none();
} Seems like the |
Beta Was this translation helpful? Give feedback.
-
Well, this is weird, i had this issue too because my provider was being disposed immediately the button was clicked before any async function was called (you can check this by simply logging some results in the onDispose function of the build method).
I got to understand that, since the providers are autoDispose by default, since you aren't explicitly listening/watching the provider anywhere in your widget, the provider is inactive and becomes disposed immediately the button is clicked. So I fixed this by keeping the provider active by at least watching it's state in the build method like so
you could then use the searchProds AsyncValue to build your widget based on its state... or not |
Beta Was this translation helpful? Give feedback.
-
The solution for me was to add @override
FutureOr<void> build() {} The good code (issue fixed) @override
FutureOr<void> build() async {} |
Beta Was this translation helpful? Give feedback.
-
"Future already completed" occurs when AsyncValue.guard is used in AsyncNotifier.
Below is a sample and the error that occurred. Is there a solution to this problem?
Beta Was this translation helpful? Give feedback.
All reactions