Easy Way to Create a Good Error-Handling in Flutter With Dartz | by Aditya Rohman | May, 2022

Don’t make your consumer confused, create a superb error-handling when creating an app

As builders, our principal process is to create purposes to unravel sure issues. When revealed, the applying we develop could produce some errors in entrance of the consumer. For instance, possibly our customers enter information incorrectly, the connection is misplaced, and even our server is down.

A great software will be capable to deal with the error and might level the consumer on the proper path once more. We definitely don’t need simply because the app can’t deal with minor errors like misplaced connection, the consumer decides to uninstall it.

On this article, I’ll present easy methods to create good error dealing with in a Flutter venture utilizing Dartz.

First issues first, we have to work out what errors are doable that may occur. In a login movement, for instance, the widespread error is:

  • Person inputs flawed password or e mail not registered
  • The server possibly had an issue
  • Misplaced web connection

Now, let’s add the bundle that we have to the pubspec.yaml file:

...
dependencies:
flutter:
sdk: flutter

dartz: ^0.10.1
...

Contained in the Dartz bundle, we are going to use one thing referred to as Both<L,R> class. This class can be used to return 2 various things in a perform.

There’s a perform with the kind Future<Both<LoginFailure, bool>>. With that kind, now I can return 2 various things out of that perform.

When the request is profitable (with standing code 200), boolean true can be returned as the proper worth.

If a request failed, I can simply deal with it by returning the enum because the left worth. The enum class act as a helper to create circumstances.

And that is the way in which we eat the login perform, for instance in a controller:

Now you may create some consumer interactions like this.

Instance consumer interactions to deal with some errors

So, that is the way in which to implement a superb error-handling on Flutter. The time period good means our app has extra wealthy error-handling to assist the consumer perceive if there’s something flawed after they use the app.

Thanks for studying. I hope these quick suggestions can assist you write a extra clear, concise, and strong codebase in your Flutter venture.

More Posts