Best design pattern for REST api based app in flutter?

xiong_sir 注册会员
2023-01-26 06:54

There is no right or wrong answer to your question. If you ask three developers, you will probably get four different answers. I'd say you can use any architecture style or design pattern which suites to your taste and requirements.

If you create an abstraction layer for the REST API (Layer 1), you could even switch to another architecture style without any change in Layer 1. Or the other way round, if you want to change your backend to Firebase or something else, you could provide another implementation of Layer 1 without any change in the frontend code (Layer 2). I'd call Layer 1 "services". The Dart API for those services should not leak out anything related to the HTTP protocol to Layer 2, but only use data types from your domain or the Dart SDK.

In that concept, the Service is an interface. The Widget has only a dependency on the interface. The actual implementation is provided to the Widget using dependency injection. Only the main.dart file knows which implementation should be used for the respective Service, and initializes the selected dependency injection mechanism accordingly. The Widget can then use any state management library and call the Service for reading and writing operations when necessary.

For the dependency injection of the Service into the Widget you can use e.g. constructor injection, inherited widget, Provider, get_it, or global variables.


About the Author

Question Info

Publish Time
2023-01-26 06:54
Update Time
2023-01-26 06:54

Related Question