oxidized 1.0.0 copy "oxidized: ^1.0.0" to clipboard
oxidized: ^1.0.0 copied to clipboard

outdated

A library of Rust-like types, including Option and Result.

example/oxidized_example.dart

import 'dart:io';
import 'package:oxidized/oxidized.dart';

Result<String, Exception> readFileSync(String name) {
  return Result(() {
    return File(name).readAsStringSync();
  });
}

Future<Result<Future<String>, Exception>> readFile(String name) async {
  return Result(() async {
    return await File(name).readAsString();
  });
}

void main() async {
  var result = readFileSync('README.md');

  // you can use switch like so
  switch (result.type()) {
    case ResultType.err:
      print('oh no, unable to read the file!');
      break;
    case ResultType.ok:
      print('read the file successfully');
      // it is safe to call .unwrap() here
      break;
  }

  // or you can use the match function
  result.match((text) {
    print('first 80 characters of file:\n');
    print(text.substring(0, 80));
  }, (err) => print(err));

  // using the "catching" constructor with futures is also feasible
  var futureResult = await readFile('LICENSE');
  var text = await futureResult.unwrap();
  print('\nlength of LICENSE file: ${text.length}');
}
46
likes
0
pub points
91%
popularity

Publisher

unverified uploader

A library of Rust-like types, including Option and Result.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on oxidized