rust_like_result 1.1.1 copy "rust_like_result: ^1.1.1" to clipboard
rust_like_result: ^1.1.1 copied to clipboard

outdated

The Result<T> type implemented in Ok<T> and Err<T> types with unwrap methods.

example/example.dart

import 'dart:io';

import 'package:rust_like_result/rust_like_result.dart';

void main() async {
  
  // Ok:
  Result<Map> res = Ok({'Forward': 'yes', 'Back': 'no'});
  //Instance of 'Ok<Map<dynamic, dynamic>>' isOk:true isErr:false val:{Forward: yes, Back: no} :
  print('$res isOk:${res.isOk} isErr:${res.isErr} val:${res.val}');
  print(res is Ok ? 'Ok' : 'Err'); // Ok
  print(res.unwrap); // {Forward: yes, Back: no}
  print(res.unwrapOrDefault({})); // {Forward: yes, Back: no}
  print(res.unwrapOrElse( (e) => {'error':e} )); // {Forward: yes, Back: no}
  

  // Err:
  res = Err(ArgumentError('Bad name'));
  // Instance of 'Err<Map<dynamic, dynamic>>' isOk:false isErr:true val:null
  print('$res isOk:${res.isOk} isErr:${res.isErr} val:${res.val}');
  print(res is Ok ? 'Ok' : 'Err'); // Err
  // print(res.unwrap); // raises exception 'Invalid argument(s): Bad name'
  print(res.unwrapOrDefault({})); // {}
  print(res.unwrapOrElse( (e) => {'Got error':e} )); // {Got error: Invalid argument(s): Bad name}


  // tryAsResult...() - calls function and return result as Ok, or Err if exception was raised:
  Result ok = await tryAsResultAsync( () => File('notExistsFile').rename('newPath')); 
  print(ok); // Instance of 'Err<dynamic>'
  ok = await tryAsResultAsync( () => Directory('.').list()); 
  print(ok); // Instance of 'Ok<dynamic>'
  ok = tryAsResultSync(() => File('notExistsFile').renameSync('newPath'));
  print(ok); // Instance of 'Err<dynamic>'
  ok = tryAsResultSync(() => Directory('.').listSync());
  print(ok); // Instance of 'Ok<dynamic>'

}
6
likes
0
points
137
downloads

Publisher

unverified uploader

Weekly Downloads

The Result<T> type implemented in Ok<T> and Err<T> types with unwrap methods.

Homepage

License

unknown (license)

More

Packages that depend on rust_like_result