Functional Error Handling In Flutter
The Dart language offers natives like try, catch, and throw for dealing with errors and particular cases in our applications.
This blog will explore the Functional Error Handling In Flutter. We will learn how to execute a demo program and understand error handling by learning Either type from the fpdart package in your flutter applications.
If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.
Table Of Contents::
What is Functional Programming?
Either & the tryCatch Factory Constructor
What is Functional Programming?
Functional programming (or FP) is an intriguing point that advances utilizing pure functions, immutable data, and a revelatory programming style, assisting us with composing all the more perfect and viable code.
This is rather than object-arranged programming (OOP), which depends on variable states and a basic programming style to depict the information and conduct of the items in our framework.
Since numerous advanced languages support both functional and object-situated ideal models, you can take on one style or the other as you see fit in your code.
In fact, you may have already used FP in your Dart and Flutter code by:
- passing a function as an argument to another capability, (for example, a > callback)
- utilizing the map, where, reduce functional operators on Iterable sorts like list and streams
- working with generics and type inference
Demo: Parsing a Number
If we have any desire to parse a String containing a numerical value into a double, we can compose code like this:
final value = double.parse('155.50');
However, what would happen if we tried running this?
final value = double.parse('not-a-number');
This code throws an exception at runtime.
In any case, the mark of the parse function doesn’t let us know this, and we need to peruse the documentation to find out:
static double parse(String source);
If we want to handle the FormatException
, we can use a try
/catch
block:
try {
final value = double.parse('not-a-number');
// handle success
} on FormatException catch (e) {
// handle error
print(e);
}
However, on large codebases, it’s challenging to figure out what abilities could throw and which don’t.
Ideally, we accept the signature of our functions ought to make it unequivocal that they can return an error.
Either type:
Either type from the fpdart package allows us to determine both the disappointment and achievement types as a component of the capability signature:
import 'package:fpdart/fpdart.dart';
Either<FormatException, double> parseNumber(String value) {
try {
return Either.right(double.parse(value));
} on FormatException catch (e) {
return Either.left(e);
}
}
The values inside Either. left and Error. right should match the sort explanations we have characterized (FormatException and double for this situation). Continuously use Either. left to represent errors, and Either. right to represent the return esteem (achievement).
Comparing Either and the Result:
From the beginning, Either is the same as the Result type that is accessible in the multiple_result package:
Result<FormatException, double> parseNumber(String value) {
try {
return Success(double.parse(value));
} on FormatException catch (e) {
return Error(e);
}
}
In fact, only the basic syntax changes:
Either.right
↔Success
Either.left
↔Error
But Either
has a much more extensive and powerful API.
Either & the tryCatch Factory Constructor:
If we have any desire to improve on our execution, we can utilize the tryCatch factory constructor:
Either<FormatException, double> parseNumber(String value) {
return Either.tryCatch(
() => double.parse(value),
(e, _) => e as FormatException,
);
}
This is how tryCatch
is implemented:
factory Either.tryCatch(
R Function() run, L Function(Object o, StackTrace s) onError) {
try {
return Either.of(run());
} catch (e, s) {
return Either.left(onError(e, s));
}
}
Note that the onError
callback gives the error and stack trace as arguments, and the error type is Object
.
But since we know that the double.parse
the function can only ever throw a FormatException
, it’s safe to cast e
as a FormatException
in our parseNumber
function.
Conclusion:
I hope this blog will provide you with sufficient information on Trying Functional Error Handling In Flutter. We’ve now ventured into the world of functional programming, by learning about Either and the fpdart package.
- we can utilize Either<L, R> as an option in contrast to throwing exceptions at whatever point we need to proclaim mistakes expressly in the mark of our capabilities/strategies.
- If we use Either and don’t deal with errors, our code will not arrange. This is superior to finding errors at runtime during development.
- Either accompanies a broad Programming interface, making it simple to control our data with a valuable practical operator like map, mapLeft, fold, and numerous others.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.