dart_result 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new18

dart_result aim to emulate the Result

There are some limitations on what is available using the Dart type system, but this library does (for certain use cases) serve it's purpose quite well.

Usage #

A simple usage example:

import 'package:dart_result/dart_result.dart';

void main() {
  Result<int> truncDiv(int num1, int num2) {
    if (num2 == 0) {
      return Failure('cannot divide by zero');
    }
    return Success(num1 ~/ num2);
  }

  final successResult = truncDiv(5, 4);
  final failureResult = truncDiv(5, 0);

  final didSucceed = successResult.isSuccess;
  final didFail = failureResult.isFailure;
  final defaultVal = failureResult.getOrElse((failed) => 0);
  final matched =
      successResult.match((val) => val, (val) => 'Operation Failed');
  final mapped = successResult.mapSuccess((val) => val + 1);
  
  print('dividing 5 by 4 successful? : $didSucceed');
  // dividing 5 by 4 successful? : true
  
  print('dividing 5 by 0 failure? : $didFail');
  // dividing 5 by 0 failure? : true
  
  print('dividing 5 by 0 failed, default val provided: $defaultVal');
  // dividing 5 by 0 failed, default val provided: 0
  
  print('pattern match on $successResult returned: $matched');
  // match on Success{value: 1} returned: 1
  
  print('mapped $successResult to value of $mapped');
  // mapped Success{value: 1} to value of Success{value: 2}
}

Features and bugs #

Please file feature requests and bugs at the [issue tracker][tracker].

1.0.0 #

  • Initial version, created by Stagehand

example/dart_result_example.dart

import 'package:dart_result/dart_result.dart';

void main() {
  Result<int> truncDiv(int num1, int num2) {
    if (num2 == 0) {
      return Failure('cannot divide by zero');
    }
    return Success(num1 ~/ num2);
  }

  final successResult = truncDiv(5, 4);
  final failureResult = truncDiv(5, 0);

  final didSucceed = successResult.isSuccess;
  final didFail = failureResult.isFailure;
  final defaultVal = failureResult.getOrElse((failed) => 0);
  final matched =
      successResult.match((val) => val, (val) => 'Operation Failed');
  final mapped = successResult.mapSuccess((val) => val + 1);
  print('dividing 5 by 4 successful? : $didSucceed');
  print('dividing 5 by 0 failure? : $didFail');
  print('dividing 5 by 0 failed, default val provided: $defaultVal');
  print('pattern match on $successResult returned: $matched');
  print('mapped $successResult to value of $mapped');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_result: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_result/dart_result.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
8
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
72
Overall:
Weighted score of the above. [more]
18
Learn more about scoring.

We analyzed this package on Dec 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms:

Error(s) prevent platform classification:

Error(s) in lib/dart_result.dart: Classes can only extend other classes.

Health issues and suggestions

Fix lib/dart_result.dart. (-98.66 points)

Analysis of lib/dart_result.dart failed with 15 errors, including:

line 13 col 25: Classes can only extend other classes.

line 15 col 35: The class 'Object' doesn't have a constructor named 'first'.

line 18 col 40: The class 'Object' doesn't have a constructor named 'second'.

line 24 col 32: The getter 'value' isn't defined for the class 'Result

line 24 col 62: The getter 'value' isn't defined for the class 'Result

Maintenance issues and suggestions

Fix platform conflicts. (-20 points)

Error(s) prevent platform classification:

Error(s) in lib/dart_result.dart: Classes can only extend other classes.

The package description is too short. (-8 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
union ^0.0.2 0.0.3+1
Dev dependencies
extra_pedantic ^1.1.1+3
test ^1.6.0