搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。