Search packages, repositories and app categories

Package detailsv6.2.0

oxidized

Definitions of Rust-like types, Option and Result, to promote safer programming.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
11.3K
Likes
52
Pub points
145

Install package

flutter pub add oxidized

Package Overview

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

Oxidized provides Rust-inspired Option and Result types for Dart, enabling safer and more expressive error handling. By modeling optional values (Option) and operations that may fail (Result), it helps developers avoid null-related bugs and handle errors explicitly. These types encourage functional programming patterns, improving code clarity and robustness. The package is lightweight and integrates seamlessly into existing Dart projects across all major platforms.

Use cases

  • Handling nullable values safely
  • Replacing null checks with explicit Option usage
  • Managing function results with success/failure states
  • Writing resilient error-handling logic
  • Adopting functional programming patterns in Dart

Key features

  • Rust-like Option type
  • Rust-like Result type
  • Null safety through explicit handling
  • Functional programming utilities
  • Cross-platform compatibility

Suitable for

  • Developers seeking safer alternatives to null
  • Teams adopting functional programming styles
  • Projects requiring robust error management
  • Dart applications targeting multiple platforms
  • Those wanting to reduce runtime crashes from null references

Considerations

  • Learning curve for developers unfamiliar with Option/Result
  • Potential verbosity in simple cases
  • Requires careful integration to avoid over-engineering
  • Not a replacement for Dart's built-in null safety
  • May increase complexity in small or trivial functions

Original README

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

oxidized

A Dart package with types similar to those found in Rust, such as the Result which represents either a value or an error, and Option which either contains Some value or None.

Motivation

The Option and Result types are akin to the "sum types" found in languages such as OCaml and Rust. They encourage safer code by making errors and possible "null" values (i.e. None) a necessary part of the code flow, requiring you to deal with these cases appropriately. For an illustration of how this style of coding can prove to be helpful, see the excellent tutorial series by Matt Rešetá Flutter TDD Clean Architecture Course – Entities & Use Cases in which he uses the Either type from the dartz package to represent either a success or a failure. The Result type in this package is specifically designed for this purpose, making it easy to remember which part is the "success" and which is the "failure" (imagine trying to remember "Is the right side the failure? Or was it the left?").

Usage

A simple example using synchronous I/O to avoid nested futures is shown below. For additional examples, see the Dart code in the example directory.

import 'dart:io';
import 'package:oxidized/oxidized.dart';

Result<String, Exception> readFileSync(String name) {
  return Result.of(() {
    return File(name).readAsStringSync();
  });
}

void main() {
  var result = readFileSync('README.md');

  result.match((text) {
    print('first 80 characters of file:\n');
    print(text.substring(0, 80));
  }, (err) => print(err));

  // or in a more functional way:
  final msg = result.when(
    ok: (text) => 'first 80 characters of file:\n$text.substring(0, 80)',
    err: (err) => err,
  );
  print(msg);
}

Prior Art

The dartz package offers many functional programming helpers, including the Either type, which is similar to Result, with the difference being that it represents any two types of values.

The either_option package has both Either and Option and supports all of the typical functional operations.

The result package offers a few basic operations and may be adequate for simple cases.

The rust_like_result offers a simple Result type similar to the one in Rust.

The simple_result package provides a similar Result type based on the type of the same name in Swift.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package