Functional Error Handling in Flutter & Dart (#2 – Either, Task, FP)

preview_player
Показать описание
📗 Learn from the written tutorial 👇👇

🎯 The biggest Flutter conference in Europe:

📧 Get Flutter news 📰 and resources:

👨‍💻 Do you write good code? Find out now!

From the first part, you already know that you shouldn't catch every exception with a blanket catch statement and you also learned how to get error messages over to your state management solution, such as a ChangeNotifer. Some may say "That's it!", but as proper software developers we should prefer hard rules over conventions.

Currently, we won't get any compile-time or even run-time errors when we don't display an error message to the user. Secondly, we treat error flow and "success data" flow separately - one through exceptions, another through return values. Similarly, we store the "success data" and its associated Failure in two distinct fields in the ChangeNotifier... Is there a way to join them?

Go to my website for more information, code examples, and articles:

Follow me on social media:
Рекомендации по теме
Комментарии
Автор

You can also create an extension for *attempt()* which would catch only Failures. This way, you prevent unneeded exceptions from being thrown, thus possibly bettering performance.

ResoCoder
Автор

I am so happy to see FP paradigm making its way to Dart. It was my biggest concern so far, that Dart is very OOPL. With data types such as Either life should be a lot easier thanks to compiler forcing the dev to handle all possible scenarios.

tomaszsosinski
Автор

Great tutorial. But please do a more advanced and related more to the real world cases such as, how to implement refresh token and retry on API calls in clean architecture. I've been trying this out but it seems like I can't think of the best way to implement it.

tityseptiani
Автор

thanks for such a good tutorial (2023)

ismoilazamov
Автор

I got a concern.
I am not sure how is in Dart but other program languages have performance penalties using throwing exception, which once hit me hard. From that moment I am trying to avoid generating additional exceptions. So I would prefer in your .map implementation instead of try catch - verifying if the obj is Failure and if isn't throw obj.
In this example performance penalty will never be visible, but in case you have iteration over many elements it will need improvement.
On other hand benefits of proposed change are better readability of code and avoiding creating bad habits.
Maybe I am wrong? Does anybody else have similar concerns?


BTW: nice video. I like most of your ideas in general. Currently going over your channel searching for additional interesting materials.

veljac
Автор

I'm glad that someone is doing the same as me - although I have my own Either class, but maybe I should give dartz another try. Hopefully it will become even better when ASTs will land in dart soon. One hint - instead of "if" when building your layout, use "switch", this way compiler will complain, if you forget to check all states from you notifier.


also - see you on Flutter Europe!

gorudonu
Автор

Always awesome. Dear Reso Coder, I've learn a lot from those tutorials you've uploaded, Thank you very much!!!. If I can make a request, please, please, please make tutorials regarding functional programming in android with Arrow and Kotlin.

taslimfi
Автор

I like the Either class, because you have to implement both cases where you want to use it. But I don't get why I would like to use a Task.
I think the code was much more readable with the try catch and you could have simply extracted it into a method. But with this you are using a system that breaks your simple readable error handling introduces a lot of functions, and you even have to introduce another cast to get basically the same result.
Like I said, I don't see a benefit there. Did I miss something?

Devenias
Автор

Great tutorial and perfect timing! Will there be a third part? i'm not exactly sure how to serve cached data in case of failure without major changes to the notifier on setting it into the viewmodel state... for testing purposes would also be great to extract the state notifier in another ChangeNotfier and inject it with dependencies.

CCGreenParadise
Автор

If anybody see this comment please help me. I want work with a list instead of single object . In case of pagination I received first list then how I can combine newly got list and previous list in either . By the way the tutorial was amazing expecting a lot from you resocoder

rahulmp
Автор

and what happens if you want to cast to the right? There is no rightMap?

pedroluzio