Return a result ErrorOr with either a value T or an error Object.

Overview

ErrorOr

Return a result ErrorOr with either a value T or an error Object.

Features

Always return a value ErrorOr from an async function. Let the called function handle the error and return an ErrorOr. The caller can then await the result without a try catch.

A subclass of ErrorOr contains either the expected value T or an error Object (usually an Exception returned from a try catch).

Internally, we create a private subclass of ErrorOr, either a _ValueWrapper or an _ErrorWrapper, with the expected non-null value.

ErrorOr can be used for non-async functions too - for both Flutter and Dart.

Getting started

Make a function return a Future<ErrorOr>, which you'll await in the calling function.

Create a ErrorOr instance by calling one of its factory constructors value or error.

Check hasError or hasValue, before calling error or value. If either is called without the proper check, a ErrorOrTypeError is thrown.

Usage

Example

Future<ErrorOr<LocationPermission>> checkPermission() async {
  try {
    return ErrorOr.value(await Geolocator.checkPermission());
  } catch (e) {
    return ErrorOr.error(e);
  }
}
ErrorOr<LocationPermission> errorOrPermission = await checkPermission();
if (errorOrPermission.hasError) {
  return errorOrPermission;
}
LocationPermission permission = errorOrPermission.value;

Additional information

I would like to keep this package minimal, but please get in touch on github if you have suggestions to improvements.

The name was inspired by the ErrorOr type of SerenityOS.

The Success/Failure pattern was inspired by result_type.

You might also like...

App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Nov 1, 2022

A package help you to make api call and handle error faster, also you can check for internet before call api.

http_solver ##not for production use, only for learning purpose. A package help you to make api call and handle error faster, also you can check for i

Jun 18, 2020

20+ Error State For Android and iOS - Flutter

 20+ Error State For Android and iOS - Flutter

20+ Error State For Android and iOS - Flutter Watch it on YouTube We design 21 error pages for your app it runs both Android and iOS because it builds

Jan 6, 2023

A web-safe implementation of dart.io.Platforms. Helps avoid the "Unsupported operation: Platform._operatingSystem" runtime error.

Universal Platform - A Web-safe Platform class Currently, if you include the dart.io.Platform anywhere in your code, your app will throw the following

Nov 20, 2022

A collection of stylish animated dialogs like Normal, Progress, Success, Info, Warning, and Error for flutter.

A collection of stylish animated dialogs like Normal, Progress, Success, Info, Warning, and Error for flutter.

stylish_dialog A collection of stylish animated dialogs like Normal, Progress, Success, Info, Warning, and Error for flutter. Showcase ⭐ Installing de

Nov 8, 2022

A Flutter Material Button that animates between Progress and Error states

A Flutter Material Button that animates between Progress and Error states

progress_button A Material Flutter Button that supports progress and error visuals Getting Started ProgressButton is designed to be easy to use and cu

Sep 21, 2022

🧾 Flutter widget allowing easy cache-based data display in a ListView featuring pull-to-refresh and error banners.

Often, apps just display data fetched from some server. This package introduces the concept of fetchable streams. They are just like normal Streams, b

Jan 18, 2022

Flutter Dropdown Alert help to notify to user when success, warning or error like push notification

Flutter Dropdown Alert help to notify to user when success, warning or error like push notification

flutter_dropdown_alert A dropdown alert package for flutter Dropdown alert will help to notify to user when you call api success, error or something l

Dec 17, 2022

Flutter error catching & handling plugin. Handles and reports exceptions in your app!

Flutter error catching & handling plugin. Handles and reports exceptions in your app!

Catcher Catcher is Flutter plugin which automatically catches error/exceptions and handle them. Catcher offers multiple way to handle errors. Catcher

Jan 7, 2023
Return a Stream that emits null and done event when didChangeDependencies is called for the first time.

did_change_dependencies Author: Petrus Nguyễn Thái Học Return a Stream that emits null and done event when State.didChangeDependencies is called for t

Petrus Nguyễn Thái Học 5 Nov 9, 2022
A widget based on Flutter's new Interactive Viewer that makes picture pinch zoom, and return to its initial size and position when released.

pinch_zoom A widget based on Flutter's new Interactive Viewer that makes picture pinch zoom, and return to its initial size and position when released

Teun Kortekaas 36 Dec 30, 2022
TheMathU Similarity Index App will accept a mathematical problem as user input and return a list of similar problems that have memorandums.

Technologies MathU Similarity Index - Segmentation Cult The MathU Similarity Index App accepts a mathematical problem as user input and returns a list

COS 301 - 2022 7 Nov 2, 2022
:end: A dart package to append either `&` or `and` at the end of a List.

And Game A dart package to append either & or and at the end of a List. Features Appends & or and at the end of the list. Available as method or exten

Tirth 2 Sep 24, 2021
This library allows you to create editable tables and spreadsheets with ease, either by providing initial row and column count to display an empty table or use it with predefined rows and column data sets.

Editable ⚡️ A highly customizable, editable table package for Flutter projects. Specs This package allows you to create editable tables and spreadshee

Godwin Asuquo 94 Dec 7, 2022
A Flutter Result type that feels like a Freezed union.

Freezed Result A Result<Success, Failure> that feels like a Freezed union. It represents the output of an action that can succeed or fail. It holds ei

Day Logger, Inc. 2 Nov 24, 2022
Kids Restaurant is an app that helps people to decide what to cook either for breakfast

Kids Restaurant is an app that helps people to decide what to cook either for breakfast, lunch, or dinner, the app gives the user variety of recipes in each section and allows the user to display the recipes written or as a video.

Eng-Mohamed Elsayed 3 Dec 19, 2022
Flutter package for Android and iOS allow you to show a wide range of hyperlinks either in the input field or in an article view

Tagtly package help you to detect a lot of hyperlink text such as.. email, url, social media tags, hashtag and more either when user type in text field or when appear a text read only.

Mohamed Nasr 4 Jul 25, 2022
will cover the GetX Named Route, GetX Route Transition, GetX Route Result, GetX Route Argument, GetX Route Parameter etc.

getx_playground 1-navigation #2-reactiv A new Fl 3-SimpleStateManagement 4-GetXControllerExample 5- DependencyExample 6-TranslationExample 7-ThemeExam

null 4 Nov 11, 2022
Ruqe brings the convenient types and methods found in Rust into Dart, such as the Result, Option, pattern-matching, etc.

ruqe Ruqe brings the convenient types and methods found in Rust into Dart, such as the Result, Option, pattern-matching, etc. Additionally, the librar

Alexander Nitiola 12 Dec 28, 2022