Search packages, repositories and app categories

Package detailsv6.1.0+1

optional

An implementation of the Optional type, which helps avoid dealing with null references

AndroidiOSWebmacOSWindowsLinux
30-day downloads
299
Likes
38
Pub points
150

Install package

flutter pub add optional

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The optional package provides a safe, functional approach to handling potentially null values in Dart using the Optional type pattern. Inspired by Java's Optional, it helps developers avoid null reference errors by encapsulating values that may or may not be present. Instead of returning null, methods return an Optional instance, encouraging explicit handling of absence. This promotes cleaner, more predictable code and reduces runtime crashes due to nulls. The package integrates seamlessly with Dart’s null safety features.

Use cases

  • Handling method returns that might be null
  • Avoiding null checks with chained operations
  • Improving code readability in data pipelines
  • Managing configuration values that may be missing
  • Implementing safe database or API response parsing

Key features

  • Null-safe value wrapping
  • Method chaining with map/flatMap
  • Safe extraction with getOrElse
  • Support for functional programming patterns
  • Composable and immutable design

Suitable for

  • Developers using null safety in Dart
  • Teams aiming to reduce null-related bugs
  • Projects requiring clean error handling
  • Functional-style Dart codebases
  • Applications with complex data flow

Considerations

  • Adds slight overhead for simple cases
  • Requires learning new pattern
  • Not suitable for performance-critical paths
  • May increase code verbosity for trivial cases
  • Best used in conjunction with null safety

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package