Ruqe brings the convenient types and methods found in Rust into Dart, such as the Result, Option, pattern-matching, etc.

Related tags

Templates ruqe
Overview

ruqe

Ruqe brings the convenient types and methods found in Rust into Dart, such as the Result, Option, pattern-matching, etc. Additionally, the library provides an excellent concept for modeling errors without resorting to exceptions.

Getting started

In your Dart/Flutter project, add the dependency to your pubspec.yaml

dependencies:
  ruqe: ^1.1.0

import with

import 'package:ruqe/ruqe.dart';

Basic usage

#[ Recoverable and Unrecoverable Errors ]

In Ruqe, there is a clear distinction between recoverable and unrecoverable errors.

1. Recoverable errors:

Recoverable errors do not cause a program to terminate completely. Ruqe makes unrecoverable error recoverable by matching the returned value of type Result and Option with the .match<T> convenient method.

void main() {
  /// Calling [stringToNum] with an alphanumeric
  /// data will trigger an exception.
  var trigger = stringToNum("%65");

  /// With pattern matching, we were able to recover from the
  /// exception thrown from calling [int.parse()] and return
  /// 0 instead.
  var result = trigger.match<int?>(
    ok: (value) => value,
    err: (_) => 0,
  );

  print("Result: $result"); // Result: 0
}

Result<int, String> stringToNum(String str) {
  try {
    var value = int.parse(str);
    return Ok(value);
  } catch (err) {
    return Err("Value is none");
  }
}

2. Unrecoverable errors:

Unrecoverable errors cause a program to terminate immediately. In Ruqe, [Panic] terminates the program when it comes across with an unrecoverable error.

What can trigger a panic?

typedef ListMap = List<Map<String, String>>;
typedef AppError = String;

var data = [
  {"first_name": "Tunbnad", "last_name": "Smart"},
  {"first_name": "Lambo", "last_name": "Turnner"}
];

void main() {
  var firstNames1 = getFirstName(data);
  print(firstNames1.unwrap()); // [Tunbnad, Lambo]

  /// Unwrapping a value of [Option] that is [None]
  /// will trigger a [Panic] and terminate the program.
  var firstNames2 = getFirstName([]);
  firstNames2.unwrap(); // panic with `[error]: an error occured!`
}

/// Returns a [Result] that is [Err] if [ListMap] is empty.
Result<List<String?>, AppError> getFirstName(ListMap data) {
  if (data.isNotEmpty) {
    return Ok(data.map((user) => user["first_name"]).toList());
  } else {
    return Err("[error]: an error occured!");
  }
}
You might also like...

Iosish indicator - 🍎 Create awesome and simple iOS style floating indicator which can be found when silent/sleep mode switched on Flutter.

Iosish indicator - 🍎 Create awesome and simple iOS style floating indicator which can be found when silent/sleep mode switched on Flutter.

Iosish indicator - 🍎 Create awesome and simple iOS style floating indicator which can be found when silent/sleep mode switched on Flutter.

Apr 1, 2022

Lost and Found is an app to help people find their lost items.

lost_and_found A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started i

Jan 20, 2022

Pianokeysgame - A simple clone of the Grant Piano Keys game found at Dave and Busters Arcade

Pianokeysgame - A simple clone of the Grant Piano Keys game found at Dave and Busters Arcade

pianokeysgame Just a simple clone of the Grant Piano Keys game found at Dave and

Feb 7, 2022

User auth form - Signup and signin user auth form with ability to stay signed in and have an option to signout.

user_auth_form SIgnup and signin user authentification form Getting Started This project is a starting point for a Flutter application. A few resource

Jan 6, 2022

Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.

Dart Sealed Class Generator Generate sealed class hierarchy for Dart and Flutter. Features Generate sealed class with abstract super type and data sub

Jan 2, 2023

A sample project for following along a tutorial found on jap.alekhin.io.

A sample project for following along a tutorial found on jap.alekhin.io.

Langaw Langaw is an endless tap game featured in the game development tutorial by Japa Alekhin Llemos in jap.alekhin.io. The tutorial series is split

Oct 16, 2022

Return a result ErrorOr with either a value T or an error Object.

ErrorOr Return a result ErrorOr with either a value T or an error Object. Features Always return a value ErrorOr from an async function. Let the calle

Nov 6, 2022

A Flutter Result type that feels like a Freezed union.

Freezed Result A ResultSuccess, Failure that feels like a Freezed union. It represents the output of an action that can succeed or fail. It holds ei

Nov 24, 2022

My-First-Flutter-App - Startup Name Generator App with favorites option

My-First-Flutter-App - Startup Name Generator App with favorites option

Startup Name Generator App with 'favorites' option.. This is my first Flutter ap

Jan 21, 2022
Owner
Alexander Nitiola
Flutter Mobile Developer | Rustacean πŸ¦€
Alexander Nitiola
A dart package for pattern matching.

A dart package for pattern matching. Inspired by match in Rust and when in Kotlin Usage var x = 2; var result = match( x, { 1: () => "Its a on

Sabin 0 Dec 30, 2021
will cover the GetX Named Route, GetX Route Transition, GetX Route Result, GetX Route Argument, GetX Route Parameter etc.

getx_playground 1-navigation #2-reactiv A new Fl 3-SimpleStateManagement 4-GetXControllerExample 5- DependencyExample 6-TranslationExample 7-ThemeExam

null 4 Nov 11, 2022
Extract pubspec details (such as package version, author and description) into Dart code.

build_pubspec This package helps you convert fields from your pubspec.yaml file into Dart code. Based on the fields in your pubspec, this package will

dartside.dev 9 Jul 15, 2021
Vineet Kalghatgi 32 May 13, 2022
Package your Flutter app into OS-specific bundles (.dmg, .exe, etc.) via Dart or the command line.

flutter_distributor Package your Flutter app into OS-specific bundles (.dmg, .exe, etc.) via Dart or the command line. The flutter_distributor source

LeanFlutter 416 Dec 24, 2022
The Import Lint package defines import lint rules and report on lints found in Dart code.

Why import lint? The Import Lint package defines import lint rules and report on lints found in Dart code. ?? Usage Add import_lint as a dev_dependenc

ryo 15 Dec 9, 2022
Flutter After Layout - Brings functionality to execute code after the first layout of a widget has been performed

Flutter After Layout - Brings functionality to execute code after the first layout of a widget has been performed, i.e. after the first frame has been displayed. Maintainer: @slightfoot

Flutter Community 432 Jan 3, 2023
A plugin that brings native iOS keyboard behavior to Flutter.

iKeyboard A plugin that brings native iOS keyboard behavior to Flutter. Getting Started Just put IKeyboard as MaterialApp ancestor and put IKeyboard.b

Jonny Borges 1 May 4, 2022
A flutter plugin that brings an in-background android app to the foreground

bg_launcher A flutter plugin that brings an in-background android app to the foreground (Android only) Restrictions on starting activities from the ba

Iheb Briki 5 Nov 25, 2022