What is the advantage of using dependency injection with providers in Flutter's riverpod?

Viewed 52

I often see people talking about using riverpod when defining dependencies for each class in flutter.

final apiClientProvider = Provider.autoDispose(
  (_) => GithubApiClientImpl(),
);

final githubRepositoryProvider = Provider.autoDispose(
  (ref) => GithubRepositoryImpl(ref.read(apiClientProvider)),
);

final repositoryListViewModelProvider = StateNotifierProvider.autoDispose(
  (ref) => RepositoryListViewModel(ref.read(githubRepositoryProvider)),
);

However, I do not understand the benefits of why it is a good idea to use riverpod. Shouldn't it be initialized at the time of use as shown below?

I feel that the following writing style is sufficient in terms of testing, for example, you can initialize the mock with a test file.


final githubRepository = GithubRepositoryImpl(GithubApiClientImpl());

final repositoryListViewModel = RepositoryListViewModel(GithubRepositoryImpl(GithubApiClientImpl()));

Why bother using riverpod?

0 Answers
Related