optional 3.0.0+2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 90

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

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();
}

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

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

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

void filterExample() {
  final hello = new Optional.of('hello');
  final world = new Optional.of('world');
  final name = new 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 = new Optional.of('a string');
  empty.ifPresent(print); // does nothing
  string.ifPresent(print); // prints "a string"
}

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

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

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

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

  stillEmpty = anEmpty.flatMap((v) => new 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(() => new ArgumentError('expected')));
  } on ArgumentError catch (e) {
    print(e); // prints "Invalid argument(s): expected"
  }

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

void valueExample() {
  final one = new 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"
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  optional: ^3.0.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
3.0.0+2 Aug 9, 2018 Go to the documentation of optional 3.0.0+2 Download optional 3.0.0+2 archive
3.0.0+1 Jul 30, 2018 Go to the documentation of optional 3.0.0+1 Download optional 3.0.0+1 archive
3.0.0 Jul 30, 2018 Go to the documentation of optional 3.0.0 Download optional 3.0.0 archive
2.0.2 Jun 26, 2018 Go to the documentation of optional 2.0.2 Download optional 2.0.2 archive
2.0.1+2 Jun 6, 2018 Go to the documentation of optional 2.0.1+2 Download optional 2.0.1+2 archive
2.0.1+1 Jun 6, 2018 Go to the documentation of optional 2.0.1+1 Download optional 2.0.1+1 archive
2.0.1 Jun 6, 2018 Go to the documentation of optional 2.0.1 Download optional 2.0.1 archive
2.0.0 Jun 6, 2018 Go to the documentation of optional 2.0.0 Download optional 2.0.0 archive
1.2.3+1 Dec 15, 2015 Go to the documentation of optional 1.2.3+1 Download optional 1.2.3+1 archive
1.2.3 Dec 15, 2015 Go to the documentation of optional 1.2.3 Download optional 1.2.3 archive

All 19 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
79
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]
90
Learn more about scoring.

We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:optional/optional.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
Dev dependencies
mockito >=2.2.0 <4.0.0
test >=0.12.0 <2.0.0