arab abdo
arab abdo

Reputation: 130

Design patterns & Clean architecture in flutter

When I start programming a new application in flutter everything goes well at the beginning but when the project grows up it starts to become messy, and then I decide to delete the project to start over. I searched about the clean architecture and design patterns but I found a lot of choices such as DDD, BLoC, and so many patterns and architectures, I didn't what is the best thing to stick with every time I enter a research process that lasts forever. So I want to hear from you as professionals and expert coders what is the best thing to stick with and what should I do? please give some advice on how to deal with big projects in flutter? Thank you very much

Upvotes: 2

Views: 4547

Answers (2)

lomza
lomza

Reputation: 9706

It's a good idea to choose layer-first or feature-first approach for your project as step one. Mentioned link to Andrea's article is a great one - https://codewithandrea.com/articles/flutter-project-structure/

Then you choose whether you want to use Riverpod with providers or blocs and/or cubits. You can write an app with all of them, so I would recommend to try them first and then make a choice. For example, I wrote a Flutter app using cubits only.

Next, you can choose routing library, to make life easier with navigating to/from screens, deep linking etc. Either auto_route or go_router is good.

Also, make use of libraries for json serialization, data classes, injectables, as they save you from lots of boilerplate code.

Upvotes: 2

Baptiste Lecat
Baptiste Lecat

Reputation: 96

Firstly the Flutter project architecture is very subjective because it depends on your needs. But we can find some general principles:

  1. Use a router system : By default flutter provide a Navigator API to let you navigate through your app. But there is no real structure and when you want to use push notifications to redirect the user to your App you will be stuck. GoRouter package delivers a very nice router system, and Google teams are thinking about implementing it into the Flutter SDK.

  2. Take advantage of a StateManagement library : When your application will become bigger you will want to handle the state of the app more precisely. So you will be able to refresh and update the content of pages easily and without reloading the content. To do that there are a lot of packages :

  • BLoC
  • Provider
  • Cubit
  • GetX
  1. Handle the data (like API JSON Response, or SQLite Query). A huge part of a clean architecture is the way you manage the data. If you use an API you have to deal with JSON Serialization. In a small app you can do your own system to deserialize data, but you have to implement a lot of boilerplate code. I recommend to use build_runner and json_serializable packages. This stack of plugins lets you build your data classes easily without worrying about the serialization : BuildRunner will generate this code for you.

  2. Use a structured folder system. Personally I used the DDD pattern, because you can easily separate your data from the logic of your app, this improves your maintainability.

Schema of a Flutter Clean Architecture with DDD

I hope this will help you to develop your own architecture. To wrap up, here are some helpful resources on this topic.

https://devmuaz.medium.com/flutter-clean-architecture-series-part-1-d2d4c2e75c47

https://docs.flutter.dev/development/data-and-backend/json

https://codewithandrea.com/articles/flutter-project-structure/

Upvotes: 6

Related Questions