'Flutter Riverpod FutureProvider.family.future does not execute/wait on 2nd attempt
I am using a button which watches a provider that executes a HTTP request like so:
Future<void> buttonPress({required WidgetRef ref}) async {
final Candidate c = ref.watch(candidateProvider);
final String candidateId = await ref.watch(postCandidateProvider(c).future);
}
And the provider is written like so:
final postCandidateProvider = FutureProvider.family<String, Candidate>((ref, candidate) async {
final uriAsString = '${unstableBaseUri.toString()}';
debugPrint('Posting candidate to $uriAsString');
final Map<String, dynamic> candidateJson = candidate.toJson();
try {
final response = await ref.read(dioProvider).post(uriAsString, data: candidateJson, onSendProgress: (int sent, int total) {
debugPrint('Sent: $sent, Total: $total');
});
debugPrint('Response: ${response.statusCode}');
final String candidateId = response.data['id'];
debugPrint('New Candidate ID: $candidateId');
return candidateId;
} on DioError catch (error) {
debugPrint('DioError when posting candidate: ${error.message}');
throw DataException.fromDioError(error);
}
});
The first time buttonPress() is clicked, there are no issues and the async call seems to wait until the FutureProvider is executed and http request comes back with a response. The second time, it does not wait at all and the candidateId returned is the same as before, even though the candidate is completely different. No errors at all. Can anybody help on why this is?
(Using Riverpod 1.0.3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
