搜索插件、代码仓与 APP 分类

插件详情v6.2.0

oxidized

Rust 风格的类型 Option 和 Result 的定义,以促进更安全的编程。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.1万
喜欢数
52
Pub 分数
145

安装插件

flutter pub add oxidized

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Oxidized 为 Dart 提供了受 Rust 启发的 Option 和 Result 类型,实现了更安全、更具表现力的错误处理。通过建模可选值(Option)和可能失败的操作(Result),它帮助开发者避免与 null 相关的错误,并显式地处理错误。这些类型鼓励使用函数式编程模式,提升了代码的清晰度和健壮性。该包轻量且可无缝集成到所有主要平台上的现有 Dart 项目中。

适合场景

  • 安全处理可空值
  • 用显式的 Option 使用代替 null 检查
  • 使用成功/失败状态管理函数结果
  • 编写健壮的错误处理逻辑
  • 在Dart中采用函数式编程模式

主要特点

  • 类似 Rust 的 Option 类型
  • 类似 Rust 的 Result 类型
  • 通过显式处理实现空安全
  • 函数式编程工具
  • 跨平台兼容性

适合对象

  • 寻求 null 安全替代方案的开发者
  • 采用函数式编程风格的团队
  • 需要强大错误管理的项目
  • 针对多个平台的 Dart 应用程序
  • 那些希望减少空引用导致的运行时崩溃的人

使用建议

  • 开发者对 Option/Result 不熟悉时的学习曲线
  • 简单情况下的潜在冗余
  • 需要仔细集成以避免过度设计
  • 不是 Dart 内置空安全的替代品
  • 可能会增加小型或简单函数的复杂性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目