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

插件详情v2.0.0

either_option

使用函数式编程概念进行错误处理的简单库

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
570
喜欢数
42
Pub 分数
150

安装插件

flutter pub add either_option

插件介绍

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

一个轻量级的 Dart 库,受函数式编程启发,提供 `Either` 类型以实现健壮的错误处理。它通过将结果表示为成功值(`Right`)或错误(`Left`)来实现安全计算。这种方法有助于避免异常,并在代码中促进显式的错误管理。该库非常适合希望在异步和函数式工作流中获得更清晰、更可预测控制流的开发者。它与 Dart 生态系统无缝集成,并支持所有主要平台。

适合场景

  • 异步操作中的错误处理
  • 函数式数据处理
  • 安全解析和验证
  • 替换 try-catch 块
  • 编写容易出错的函数
  • 构建具有弹性的业务逻辑

主要特点

  • 提供 `Either<Left, Right>` 类型
  • 支持函数式组合
  • 不可变且类型安全
  • 最小化样板代码
  • 与 async/await 兼容
  • 跨平台兼容性

适合对象

  • 使用函数式编程模式的开发人员
  • 优先考虑错误安全的团队
  • 需要干净错误传播的项目
  • Dart 开发人员避免异常
  • 那些构建可重用、可组合逻辑的人
  • 需要可预测状态转换的应用程序

使用建议

  • 需要理解函数式概念
  • 可能增加初学者的学习曲线
  • 不适合简单的错误情况
  • 仅限与显式错误类型一起使用
  • 最佳搭配其他 FP 工具
  • 在复杂的链式结构中需要仔细的类型对齐

原始 README

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

either_option

either_option is a simple library typed for easy and safe error handling with functional programming style in Dart. It aims to allow flutter/dart developpers to use the 2 most popular patterns and abstractions : Either and Option, mainly used in FP language like Scala, Haskell, OCaml,...

Installation

Prerelease versions that support null safety

Package link on pub either_option In your pubspec.yaml dependencies add

   either_option: ^2.0.0

Overview

Either

Either Represents a value of one of two possible types. By convention we put missing or error value in an instance of Left and expected success value in an instance of Right.

For example, we fetch an url from a repository repository to get an User details and use Either<ServerError,User> :

  Future<Either<ServerError, User>> getUser(int id) async {
    final baseUrl = "https://fakerestapi.azurewebsites.net/api/Users/$id";

    final res = await http.get(baseUrl);

    if (res.statusCode == 200) {
      dynamic body = json.decode(res.body);
      User user = User.fromJson(body);
      return Right(user);
    }
    if (res.statusCode == 404)
      return Left(ServerError("This user doesn't exist"));

    return Left(ServerError("Unknown server error"));
  }

So now to consume result we can use for example fold method and say what to do with value :

main() async {
  final Repository repository = Repository();

  final Either<ServerError, User> res = await repository.getUser(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.fold((_) => defaultUser.username, (user) => user.username);
  print(userName); // "User 3"
  
//if res was a Left, print(userName) would give "ko"
}

Option

Option Represents a value of one of two possible types. By convention we consider missing value as an instance of None and expected success value in an instance of Some.

  Future<Option<User>> getUserOpt(int id) async {
    final res = await http.get(baseUrl);
    return Option.cond(
        res.statusCode == 200, User.fromJson(json.decode(res.body)));
  }
  // -----------------------------------------
  main() async {
  final Repository repository = Repository();

  final Option<User> res = await repository.getUserOpt(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.getOrElse(defaultUser).username;
  print(userName); // "User 3"

  //if res was a None, print(userName) would give "ko"
}

Ressources & explanation

Medium article

Features

Functions available :

Option Either
fold :+1: :+1:
map :+1: :+1:
flatMap :+1: :+1:
getOrElse :+1:
orElse :+1:
toLeft :+1:
toRight :+1:
toEither :+1:
Option.empty :+1:
Option.of :+1:
filter :+1:
exists :+1:
contains :+1:
swap :+1:
cond :+1: :+1:

Example

Tests

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。