What is the best design pattern between BLoC, MVVM, MVC for Flutter app? - flutter

I'm going to develop a management app for a company in Flutter and since I never worked with this framework i was looking for the best practices and desin patterns to use.
The app will be about employes management for a company. The main features will be:
The management of the employes profiles;
The possibility to upload documents directly in the app;
Create online quiz for the employes in trainership;
I'm struggling to choose the right design pattern. Usually for the frontend i use either the MVC or MVVM, but since this is my first Flutter app i made some researches and find out that one of the best pattern for Flutter is BLoC. I already tried to implement a simple app to try this pattern and i understood the way it works, but since I'm a noob in Flutter i was looking for some advices from someon who is more expert than me.
Thank you in advance for the help.

the best practices and design patterns to use it is TTD
it helps you to Keeping your code clean and tested
on the other hand, larger projects start falling apart when you mix the business logic everywhere. Even state management patterns like BLoC are not sufficient in themselves to allow for easily extendable codebase.
to get more information about TTD you can look to this tutorial in this link : https://resocoder.com/2019/08/27/flutter-tdd-clean-architecture-course-1-explanation-project-structure/

Related

Flutter: State management

I've seen many tutorials praising Bloc State management, what is so special about it and should I learn it as a beginner? if not is there any beginner friendly state management technique?
BLoC/Cubit
BLoC is great for complex state management for complex apps. However, inside the BLoC library there's a simpler way of managing state that's called Cubit (Cubit is sort of a subset of BLoC). Cubit is largely the same as BLoC except:
less boilerplate
doesn't use 2-way streams
This renders it much easier to learn, and a fantastic stepping-stone into a full-out BLoC driven state management solution.
Currently, my team and I are building a very complex app, and we use the principle: use Cubit's, unless there's a specific reason to use a BLoC. This has worked well for us (85% of our app is run with Cubit, 15% with BLoC).
In relation to other state management techniques, most people are probably going to recommend Provider or Riverpods (Riverpods = Provider on steroids). They are easier to learn than Cubit/BLoC. Except, only for simple cases (a few page app). Once your app gets complex (authentication, feeds, api calls, etc.) a Cubit/BLoC-based architecture is going to scale better and be much cleaner.
Additionally, the most-used state management system for production-level Flutter apps is BLoC/Cubit. So, if you're looking for a marketable skill, I'd default to that.
Helpful links:
Flutter package that contains BOTH BLoC and Cubit.
The tutorial series I watched to learn BLoC/Cubit (and recommend!).
I HIGHLY recommend watching this series as well that shows WHERE to put your BLoC/Cubit in an app to align with best practices and clean architecture.
Example app (understanding this will help you a lot):
Here's a simple 1-feature app I made as a proof of concept to show how Cubit specifically works. Read the project's README.md for context.
Conclusion:
Provider, GetX, Riverpods, etc. are all easier to learn and contain less boilerplate than BLoC, except they won't scale as well when your app gets more complex.
To help combat the boilerplate/complexity problem of BLoC, use Cubits instead of BLoCs in your design unless you have a specific need for BLoCs.
Cubits are quite easy to understand and can be used in most of the Flutter projects. For bigger apps I would go for Riverpod. Having independent providers gives a lot of flexibility, as they can be used in different parts of the app and you can make any future, use case or repository a provider.
I have wrote a tutorial with Flutter app on how to write a List - Details app using cubits, hooks and local database with Hive.
GETX - State Management
I would suggest Getx as I have been using it for 3 years and it's incredible.
It is effortless to learn.
I never encountered a need to use any other state management.
Features provided by GETX
State management
Dependency Injection
Theming
Clean Structure
Internationalization
Validation / Utils
Documentation

Best Practices in Flutter/Dart and Clean Architecture (maintaining separation of concerns between data, domain and presentation layers)

I'm a backend developer with background of Java and Python. I've started with Flutter 3 months ago. I find Dart an easy language to learn and fun to write code in. Google did a good job there.
What is challenging for me is figuring out best practices when working with flutter. The official documentation (docs.flutter.dev) is good and it's also interesting to look at the Dart code powering the framework. But I haven't seen a resource that goes over best practices for design/architecture of a Flutter application.
In particular, I'm interested in how to maintain separation of concerns between Database , Domain and Presentation layers.
I'm using Firebase Store for my (realtime) database. The API provides the data in the form of a JSON object (Map). This is fine, but I want to translate this into my own domain model instances (for data validation, enrichment and enforcing other business rules). What I don't know is how to bridge the gap between database and UI (Widgets). I want to keep a Clean Architecture, and would like to learn how to do this correctly. I should also add that I don't have prior experience with Asynchronous code, which seems to be an important skill to gain.
I've experimented in using InheritedWidgets to try and expose my data to the UI/Presentation layer as model instances, without the UI knowing about the specifics of the database, but ran into difficulties because I don't understand the Flutter framework and Dart async programming very well. I also know there are frameworks like Bloc that are supposed to solve data/state management issues. But as much as possible I want to stick to the basics before I start using more elaborate and heavier tools.
What recommended resources are out there that I'm missing, that could help me become a better Dart/Flutter developer?

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
Firstly the Flutter project architecture is very subjective because it depends on your needs.
But we can find some general principles:
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.
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
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.
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/
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.

Hummingbird (Now Flutter for web) is planned for release. Should I stop learning AngularDart?

With the recent announcement of Hummingbird, it looks like Flutter will mature for web apps. I was trying to learn AngularDart, but I personally believe Flutter web apps will be a better approach. I'd have to plan a lot to share my codebase between Flutter and AngularDart.
Will Hummingbird receive support for Flutter web apps? Does the use of AngularDart will have diminishing returns in the future?
I understand the answers might be very subjective, but maybe it would be better to discuss some serious advantages and disadvantages of flutter-web.
Update (13.09.2019) Hummningbird is now Flutter-Web and Flutter-Web is now merged to Flutter branch. Things have changed for good ;)
As always, the answer is: It depends.
If you want to reach a lot of users quickly, AngularDart is your friend as it provides a solid foundation right now.
Also notice that doing a lot of "planning to share your codebase", which basically translates to thinking about separating your business logic from your UI logic, is a win either way because the more modular code will benefit you in the long run.
Additionally, it's never a bad thing to get to know more frameworks.
However, if you can allow yourself to be patient and you're thinking in the long term, Flutter's future support for web and Fuchsia may intrigue you.
That's why - if you believe in Flutter's success - it can make sense to only develop for Flutter and then wait and see what's about to happen.
Personally, I believe Flutter will become a well-supported, versatile, general UI framework.
That's why I would recommend betting on Flutter if your project is not time-critical.
I think it really depends on your use case. AngularDart and angular_components are trying to support a more 'enterprise' use case. Complex web apps that are primarily desktop focused. While HummingBird is focusing more on the mobile web use case or where you already have a flutter app that you happen to want to target web also with the same target. So both will have a place.
I understand you very wel ;-)
I'm now tryin to recycle my carrer and been a lot of months learning Angular (The Typescript branch) I love work with it, so pleasant an so intuitive, I did some major web apps than are working very well ant the mantainence is easy, quick and clean ... the future seemed me and Angula in a long love afaire ...
But sudenly I hear about Flutter, like two monts ago, and I was captivated for the concept (before I already had try different aproachs to have one codebase an reach all mobile platforms, Xamarin in major part, and was painfull to learn and to use, ReactNative never make me feel anything), but Flutter is soo easy, with so much power than Xcode and Swift witch I been workin too and with wich when you have a middle project the storyboard is intelligible. I been learning Flutter full time last two months, already have app in appstores and doing my first web (wonderfull experince doing web without css, ohhhh yes).
Then my opinion is similart to the others "depends", but if I was you and was just starting to learn AngularDart I will go for Flutter without loocking back.

MVVM in Windows Phone 7

Any good sample WP7 application using MVVM model in the optimal way?
I'm creating my first WP7 app, and I'm using MVVM as far as I can tell, but I'm not sure I'm doing it the right way. I have one view model per page, instead of one main view model that branches to each page, I'm not sure which is the correct way to do, so I'm hoping there's a sample app out there that I can check out.
Thanks!
Edit: I'm also having another problem on saving the ViewModels in the application state, because I think they have to be serializable (haven't worked much into this), the thing is that when I start a task and come back to the app, the latter has already been deactivated by calling the former, so I have to serialize its state when deactivated and [re]serialize it when [re]activated. This is how I save the state when deactivated:
object[] viewModels = new object[3];
viewModels[0] = App.ViewModelPage1;
viewModels[1] = App.ViewModelPage2;
viewModels[2] = App.ViewModePage3;
PhoneApplicationService.Current.State.Add("LastState", viewModels);
Again, this is probably not efficient way to do it, so I'm hoping I can see a sample app that handles this well too.
Thanks!
Have you looked at using the MVVM Light toolkit?
Serialization best practices will vary based on the volume of data in the model, the number of models being used and whether it's necessary to always load all of the models.
You might want to check out Caliburn Micro. It is used to implement a number of user experience patterns but it supports WP7 and has sample code.
Here's some MVVM samples and guidance you can check out.
C#er : IMage: Model-View-ViewModel (MVVM) Explained
.NET by Example: Using MVVM Light to drive a Windows Phone 7 / Silverlight 4 map viewer
The simplest way to do design-time ViewModels with MVVM and Blend.
Also an overview here of MVVM frameworks you may find worth a look.
JAPF » Blog Archive » Discover and compare existing MVVM frameworks
Light weight seems to be good and MVVM Light is popular. Laurent demos MVVM in the Mix 10 video EX14 if you want to check that out too.
This months MSDN magazine has an article on creating a WP7 Sudoko app using MVVM.
http://msdn.microsoft.com/en-us/magazine/gg490347.aspx
HTH
Here is my article which describes the approach to build WP7 applications using advantages of separation of concerns:
a framework for building of WP7 application