Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter best architecture patterns

Tags:

I came from MVC and VIPER world and now I am new to Flutter cross-platform development. I really like declarative things it brings (like SwiftUI for example does as well). I see a lot of advantages in React architecture that Flutter uses to update UI with the most recent data. Though I still try to understand a conception of widgets. In my head, a word widget is more about UI things, but the documentation says that all in Flutter is a widget.

Let me highlight a simple example. Also, let's forget about declarative UI things.

In the iOS world using Objective-C or Swift, we usually separate a lot of layers such as data layer, UI layer, service layer, some helper layers and etc.

As you may notice we can't simply call these layers widgets, but looks like flutter can, but I may be wrong.

In the iOS world, I would like to use VIPER or some similar architecture pattern to separate different layers or add some services which request some data for me or do save it into the database.

What are the similar approaches or architecture patterns which I can use to follow best practice recommendations to achieve the best result, because as for me if we call some service which saves data to the database as a widget it's a bit strange. I would like to call it more service rather than a widget.

Do I need to write a widget for all such things? Or did I get it wrong?

like image 231
Matrosov Oleksandr Avatar asked Aug 31 '20 20:08

Matrosov Oleksandr


People also ask

Is MVVM good for Flutter?

By default, Flutter apps don't use any specific design pattern. This means the developer is in charge of choosing and implementing a pattern that fits their needs. The declarative nature of Flutter makes it an ideal candidate for the MVVM design pattern.

What architecture is Flutter?

The architecture of flutter is comprised of four components namely Flutter engine, foundation library, widgets, design specific widgets where flutter engine is a runtime that can be portable for high quality mobile applications which is based on C++ programming language and the necessary packages for writing a flutter ...

Is Flutter BLoC Good?

Flutter bloc it's a great option, as you can see it's not complicated to use it and it's easy to understand the main concept of how can you use it. Also, It gives you a lot of ways to manage your views or widgets.

What pattern does Flutter use?

Flutter developers use various design patterns to write clean and maintainable codebases. They often write reusable widget implementations in individual Dart files, separate the main app screens into different files, and decompose large and isolated widgets into private methods/classes.


1 Answers

Do I need to write widget for all such things? Or did I get it wrong?

Let me start by saying that flutter is flexible enough to allow you to adopt any pattern you have been using before and from MVVM, MVC, Redux, Mobx, Bloc, Provider, Riverpod etc pp there are many patterns out there which you can lean on.

Q: Do all services have to be widgets?

A: It depends.

Lets talk about "getting" the services first (dependency injection)

Using Widgets is just one way of doing dependency injection in Flutter which has the huge benefit of being scoped (only available to the children of the widget) and also of being disposed when this part of the widgettree is no longer needed (a bit over simplified).

There are other DI systems like getIt which don't rely on the widget tree - and you can do fancy stuff with getIt which is cumbersome to do in many of the others which rely on the build context to provide access to the object.

Imho in most cases you would not want to have your logic/ service in a widget but in a separate class which is then "provided" via a widget (e.g. using Provider, or by injecting it via getIt into the widget).

About "best practices":

There are many cool patterns which all have their pros and cons.

If you have a big team or the developers are changing often you might want to use a more rigid and biased system like BLOC (using the bloc library) - if you are solo or have enough time on your hands to do your own research, there might be patterns which better suit your needs. There is no one size fits all answer to this question.

For further research I would point towards the Flutter Architecture samples and the corresponding Github repo, there might be more examples in the Pull Request section

like image 151
Damian K. Bast Avatar answered Sep 26 '22 02:09

Damian K. Bast