1

I am building my first RiverPod based Flutter app, and I'm trying to understand how RiverPod works with the service and repository pattern.

I have a repository class that owns the task of handling interactions with a particular storage API, in this case it can either be FireStore or Hive according to user configuration.

There is a separate service class that acts as the logical tier for persisting data, like...

final goalRepositoryProvider = Provider<FsGoalRepository>((ref)=> FsGoalRepository());

class GdGoalService {

  Future<GdGoal> add(GdGoal aNewGoal) async {
    return ref.watch(goalRepositoryProvider).add(aNewGoal);
  }
   ...
}

Problem is, I can't find a way of setting the 'ref' value into the service class. In essence, I am trying to use RiverPod to dependency inject the repository class into the service.

Is passing the 'ref' as a parameter to the service a legitimate approach?

final goalServiceProvider = Provider<GdGoalService >((ref) {   
  return GdGoalService (ref); 
});

Suggestions?

user2868835
  • 1,250
  • 3
  • 19
  • 33

1 Answers1

0

Yes, it is legimate to pass ref into your service code from its provider wrapper. The providers will get ref from each other, and ultimately from the ref.watch or ref.read of a consumer widget, which wires them all together with the ProviderScope, where all the data essentially "lives".

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70