class_switch 0.0.1-dev.3 copy "class_switch: ^0.0.1-dev.3" to clipboard
class_switch: ^0.0.1-dev.3 copied to clipboard

`class_switch_generator` lets you switch over all the sub-classes of a class instance or all possible combinations of sub-classes for multiple base class instances.

example/example.dart

import 'package:class_switch/class_switch.dart';

part 'example.g.dart';

@ClassSwitch()
abstract class Fruit {}

class Apple extends Fruit {}

class Orange extends Fruit {}

void main() {
  final myFruit = Apple();

  var result = myFruit.$switch(
      (apple) => 1, //
      (orange) => 2,);
  assert(result == 1);

  result = MyFruitSwitcher().$switch(Orange());
  assert(result == 2);

  final strResult = myFruit.$switch((apple) => 'x', (orange) => 'y');

  assert(strResult == 'x');
}

class MyFruitSwitcher extends _$FruitSwitcher<int> {
  @override
  int apple(Apple apple) {
    return 1;
  }

  @override
  int orange(Orange orange) {
    return 2;
  }
}

class MyFruitHandlerWithADefault extends _$FruitSwitcherWithDefault<String> {
  @override
  String defaultValue() {
    return 'default';
  }

  @override
  String orange(Orange orange) {
    return 'orange is special';
  }
}
1
likes
150
points
44
downloads

Publisher

verified publishernigelgott.com

Weekly Downloads

`class_switch_generator` lets you switch over all the sub-classes of a class instance or all possible combinations of sub-classes for multiple base class instances.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

meta

More

Packages that depend on class_switch