Skip to content

Command_it & Result Patterns - an Integration Doubt #9

@wolf-coder

Description

@wolf-coder

I discovered your package by following the Clean Architecture suggestions in the official Flutter documentation. The command-objects case study specifically recommended flutter_command, which led me to your work.

1. That official doc proposed a Command pattern implementation that works with the Result class pattern:

// in ViewModel
HomeViewModel({
  required BookingRepository repo,
}) : _repo = repo {
  loadCommand = Command0(_function)..execute();

late Command0 loadCommand;

Future<Result> _function() async {
  final result = await _repo.getBookingsList();
  switch (result) {
    case Ok<SomeDataClass>():
      ....
    case Error<SomeDataClass>():
      ....
  }
  return result;
}
// in View
ListenableBuilder(
  listenable: widget.viewModel.load,
  builder: (context, child) {
    if (widget.viewModel.loadCommand.running) {
      return const CircularProgressIndicator();
    }

    if (widget.viewModel.loadCommand.error) {
      ...
    }
    if (widget.viewModel.loadCommand.completed) {
      // here
    }
    return child!;
  },
)

2. The way I see result pattern works with command_it is by returning Sucess.value and throwing Failure.value:

HomeViewModel({
  required BookingRepository repo,
}) : _repo = repo {
  commandResult = Command.createAsyncNoParam(
    _function,
    initialValue: ,
  );
}

Future<SomeDataClass> _function() async {
  final Result<SomeDataClass> result = await _repo.getBookingsList();
  switch (result) {
    case Ok<SomeDataClass>():
      ....
      return result.value; <---
    case Error<SomeDataClass>():
      ....
      throw '${result.error}' <---
  }
}

However, I'm quite skeptical about this workaround long-term viability. I'm hoping for some insight for robust design pattern (Command and Result).

Lastly, kudos on your work! I'm slowly adapting flutter-it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions