I came across some code using Riverpod, for example for retrieving http data with Dio, using a Riverpod provider as below:
final client = Provider((ref) => Dio());
then use it to fetch data:
final response = await ref.watch(client).getUri<Map<String, Object?>>(uri, cancelToken: cancelToken);
Why use a Riverpod Provider for the client as it reference will never change and not use a global variable instead?:
final clientDio = Dio();
final response = await clientDio.getUri<Map<String, Object?>>(uri, cancelToken: cancelToken);
CodePudding user response:
Using a provider allows replacing the implementation with a different one using overrides.
For an HTTP client, this can allow replacing it with a fake one that returns pre-determined responses. This can be useful for testing or development/debugging purposes.