D J
D J

Reputation: 163

What is the correct way to pass repository from main.dart to widgets as per Clean Architecture?

I am trying to implement a (trial) app in Clean Architecture. It is nothing but a plain app that shows data after fetching it from an web api. If I am not mistaken(please do inform me if I am wrong), the (minimum)correct way is to have

Now I want to know if it's correct to set the repository in the main.dart file like:

void main() {
  runApp(MyApp(
      ARepository(
          AWebClientDataProvider:
          AWebClientDataProvider()
      )
  ));
}

Then I had to pass the repository to the widget. I had tried with the "Provider" package but it didn't work(since it must work within initState()). Currently, I got it working by passing the repository via the widget's constructor(Is there a better way?).

Upvotes: 3

Views: 340

Answers (1)

S. M. JAHANGIR
S. M. JAHANGIR

Reputation: 5020

It is absolutely fine and the recommend way. You should always try to do dependency injection on constructors to comply with the Dependency inversion principal of SOLID.

But it's actually tedious to manage all the dependencies manually. So it's highly recommended to use something like GetX to manage your dependencies.

Upvotes: 1

Related Questions