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...

Learn how to use Dart List Utility Methods in Flutter

Learn how to use Dart List Utility Methods in Flutter

Flutter Tutorial - List Utility Methods Learn how to use Dart List Utility Metho

Dec 29, 2021

A dart package for many helper methods fitting common situations

Basic Utils A dart package for many helper methods fitting different situations. Table of Contents Basic Utils Table of Contents Preamble Install pubs

Jan 5, 2023

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

This Flutter package provides a Search Widget for selecting an option from a data list

This Flutter package provides a Search Widget for selecting an option from a data list

Search Widget This Flutter package provides a Search Widget for selecting an option from a data list. Provides filtering of items based on the search

Oct 31, 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

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

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

Nov 25, 2022

Flutter The lightest, easiest and most convenient route management!

Flutter The lightest, easiest and most convenient route management!

Language: English | ไธญๆ–‡็ฎ€ไฝ“ nav_router nav_router is the simplest / lightweight / convenient routing management solution for flutter. It supports various

Jan 3, 2023

A convenient code generator for app styleguide, gallery, wireframes and/or storyboard.

A convenient code generator for app styleguide, gallery, wireframes and/or storyboard.

Framy A convenient code generator for app styleguide, gallery, wireframes and/or storyboard. ๐Ÿ‘‰ Official documentation ๐Ÿ‘ˆ Packages In order to use Fra

Dec 19, 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
Petrus Nguyแป…n Thรกi Hแปc 193 Dec 29, 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
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

6thSolution 15 Jan 2, 2023
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

null 0 Jan 6, 2022
Superpowers for Dart. Collection of useful static extension methods.

If you miss an extension, please open an issue or pull request Resources: Documentation Pub Package GitHub Repository On this page you can find some o

Simon Leier 955 Jan 8, 2023