optional 5.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 95

Coverage Status

Optional.dart #

Optional.dart is an implementation of the Optional type, inspired by Java 8's Optional class.

Optional helps avoid null reference errors by wrapping values in an object that holds information regarding whether the value is present or not.

For example, when applying a set of operations to a value, any of which might return null, you can get rid of the null checks by using Optional.map():

String getString() {
  // ..
  if (condition) {
    return null;
  } else {
    return "12";
  }
}

int calculateSomething(String str) {
  // ...
  if (someCondition) {
    return null;
  } else {
    return 3 + int.parse(str);
  }
} 

double calculateSomethingElse(int val) {
  // ...
  if (anotherCondition) {
    return null;
  } else {
    return val * 1.42;
  }
}

void main() {
  
  // before
  
  String str;
  int i;
  double d;
  
  str = getString();
  
  if (str != null) {
    i = calculateSomething(str);
  }
  
  if (i != null) {
    d = calculateSomethingElse(i);
  }
  
  if (d != null) {
    print(d);
  }
  
  // with Optional
  
  Optional.ofNullable(getString())
    .map(calculateSomething)
    .map(calculateSomethingElse)
    .ifPresent(print);
}

Changelog #

5.0.0 #

  • Refactored async support to orElseGetAsync() to avoid bogus dartanalyzer errors
  • (Breaking change) or*() methods no longer support async. Instead, use orElseGetAsync()

4.2.0 #

  • Added async support
    • The or*() methods now accept and return FutureOr

4.1.0 #

  • Optional now implements Iterable
    • The toSet() and toList() methods now return modifiable collections.
    • The toList() method now accepts a growable optional named parameter, to comply with the Iterable interface.

4.0.1 #

  • Fixed dartdoc generation

4.0.0 #

  • Added toOptional extension method
  • Note that this requires the minimum SDK version to be 2.6.0, please continue to use version 3.1.0 if upgrading to SDK version 2.6.0 is not an option.

3.1.0 #

  • Added contains(val) method.
  • Added toSet() method.
  • Added toList() method.

3.0.2 #

  • Fix Present cast() issue

3.0.1 #

  • Absent cast() method bug-fix

3.0.0 #

  • (Breaking change) Removed operator overloads. They were breaking strong-mode type checks.
  • (Breaking change) Made equality checks more consistent with Dart standards. Now any two empty Optionals are equal, and two non-empty Optionals are equal if their values are equal, regardless of their types.
  • Added a cast() method, similar to List.cast().

2.0.2 #

  • Better use of generic methods (no more dynamic).

2.0.0 #

  • Compatibility with Dart 2

1.2.3 #

  • Add an optional orElse parameter to ifPresent.

1.2.2 #

  • Optional.empty() constructor is now const.

1.2.1 #

  • Absent and Present are now subclasses of Optional. You should continue to use constructors in Optional, and not the Absent or Present ones.
  • EMPTY is now a top-level constant.

1.2.0 #

  • Operators are now overloaded, so you can add, subtract, etc., Optionals. Any operation involving an empty Optional will return an empty Optional.
  • Added examples.

1.1.0 #

  • Optional.ofNullable() and Optional.empty() constructors are now const.
  • Optional.of() constructor now throws ArgumentError when passed null, instead of NoValuePresentError.
  • NoValuePresentError now extends StateError.

1.0.0 #

  • Initial release

example/optional_example.dart

import 'package:optional/optional.dart';

void main() {
  emptyExample();
  filterExample();
  ifPresentExample();
  mapExample();
  orElseExample();
  valueExample();
  extensionExample();
  asyncExample();
}

void emptyExample() {
  try {
    print(empty.value);
  } on NoValuePresentError catch (e) {
    print(e); // prints "Bad state: no value present"
  }

  try {
    print(Optional.of(null));
  } on ArgumentError catch (e) {
    print(e); // prints "Invalid argument(s): value must be non-null"
  }

  final anEmpty = Optional.ofNullable(null);
  print(anEmpty.isPresent); // prints "false"
  const anotherEmpty = Optional<dynamic>.empty();
  const yetAnotherEmpty = empty;
  print(anEmpty == anotherEmpty); // prints "true"
  print(anEmpty == yetAnotherEmpty); // prints "true"
  print(anotherEmpty == yetAnotherEmpty); // prints "true"
}

void filterExample() {
  final hello = Optional.of('hello');
  final world = Optional.of('world');
  final name = Optional.of('harry');

  for (var o in [hello, world, name]) {
    final filtered = o.filter((v) => v.startsWith('h'));
    print(filtered.isPresent);
  } // prints "true", "false", "true"
}

void ifPresentExample() {
  final string = Optional.of('a string');
  empty.ifPresent(print); // does nothing
  string.ifPresent(print); // prints "a string"
}

void mapExample() {
  final helloWorld = Optional.of('hello, world');
  final hello = helloWorld.map((s) => s.substring(0, 5));
  print(hello.value); // prints "hello"

  final one = Optional.of(1);
  final two = one.map((v) => v + 1);
  print(two.value); // prints "2"

  final three = two.flatMap((v) => Optional.of(v + 1));
  print(three.value); // prints "3"

  const anEmpty = Optional<int>.empty();
  var stillEmpty = anEmpty.map((v) => v + 1);
  print(stillEmpty.isPresent); // prints "false"

  stillEmpty = anEmpty.flatMap((v) => Optional.of(v + 1));
  print(stillEmpty.isPresent); // prints "false"
}

void orElseExample() {
  print(empty.orElse(2)); // prints "2"
  print(empty.orElseGet(() => 2)); // prints "2"
  try {
    print(empty.orElseThrow(() => ArgumentError('expected')));
  } on ArgumentError catch (e) {
    print(e); // prints "Invalid argument(s): expected"
  }

  final string = Optional.of('a string');
  print(string.orElse('another string')); // prints "a string"
  print(string.orElseGet(() => 'another string')); // prints "a string"
  print(string
      .orElseThrow(() => ArgumentError('unreachable'))); // prints "a string"
}

void valueExample() {
  final one = Optional.of(1);

  print(one.value); // prints "1"
  try {
    print(empty.value); // throws NoValuePresentError
  } on NoValuePresentError catch (e) {
    print(e); // prints "Bad state: no value present"
  }
}

void extensionExample() {
  final one = 1.toOptional;
  print(one.value); // prints "1"
  print(null.toOptional.isPresent); // prints "false"
}

Future<Optional<int>> emptyFuture() {
  return Future.value(Optional.empty());
}

Future<int> intFuture(int val) {
  return Future.value(val);
}

void asyncExample() async {
  final e = await emptyFuture();
  var i = await e.orElseGetAsync(() => intFuture(1));
  print(i); // prints "1"
  i = e.orElse(await intFuture(2));
  print(await i); // prints "2"
}

Use this package as a library

1. Depend on it

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


dependencies:
  optional: ^5.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


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

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
collection ^1.14.11 1.14.13 1.15.0-nullsafety
Dev dependencies
mockito >=2.2.0 <4.0.0
pedantic ^1.0.0
test >=0.12.0 <2.0.0
test_coverage ^0.4.0