Reputation: 163
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
an entity(a class with the exact fields delivered by the web api), a data provider(a mechanism to fetch the data from the web api), a repository that represent the data handling for the app, the model class that can be used to hold the formatted data(to be loaded to widgets).
Widget that can be used to display data and the main.dart file(the entry point of the application)
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
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