micro_core_result 0.2.2 copy "micro_core_result: ^0.2.2" to clipboard
micro_core_result: ^0.2.2 copied to clipboard

This package is a simple approach to return multiple values, inspired on dartz package.

example/micro_core_result_example.dart

import 'package:micro_core_result/micro_core_result.dart';

const names = ['Johnson', 'Harvey', 'Marshall', 'Denver', 'Noelene', 'Coreen'];

class Repository {
  Future<Result<Exception, Empty>> createName(String name) async {
    try {
      if (name.isEmpty) throw Exception('An empty name is invalid!');
      await Future.delayed(const Duration(seconds: 2));
      return Right(empty);
    } on Exception catch (exception) {
      return Left(exception);
    }
  }

  Future<Result<Exception, List<String>>> listNames() async {
    try {
      await Future.delayed(const Duration(seconds: 2));
      return Right(names);
    } on Exception catch (exception) {
      return Left(exception);
    }
  }
}

class Controller {
  final Repository _repository;

  final names = <String>[];

  Controller(this._repository);

  Future<Result<Exception, Empty>> createName(String name) async {
    final result = await _repository.createName(name);

    return result(
      (e) {
        print('Exception => ${e.toString()}');
        return Left(e);
      },
      (e) {
        print('Name was created successfully!');
        return Right(e);
      },
    );
  }

  void listNames() async {
    final result = await _repository.listNames();

    result(
      id,
      (namesList) {
        print('Names before: $names');
        names.addAll(namesList);
        print('Names after: $names');
      },
    );
  }
}

void main() {
  final repository = Repository();
  final controller = Controller(repository);

  controller.createName('');

  controller.createName('Dave');

  controller.listNames();
}
4
likes
160
points
54
downloads

Publisher

verified publisherjotapetecnologia.com.br

Weekly Downloads

This package is a simple approach to return multiple values, inspired on dartz package.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

More

Packages that depend on micro_core_result