Home > database >  dart - a generic method getter or what is this
dart - a generic method getter or what is this

Time:02-13

I have come across this in moctail, what is "get when" in the function signature - a generic method getter, and why can you use it as a function in test?

When<T> Function<T>(T Function() x) get when {
  if (_whenCall != null) {
    throw StateError('Cannot call `when` within a stub response');
  }
  _whenInProgress = true;
  return <T>(T Function() _) {
    try {
      _();
    } catch (_) {
      if (_ is! TypeError) rethrow;
    }
    _whenInProgress = false;
    return When<T>();
  };
}

CodePudding user response:

when is a getter that returns a generic function. That generic function takes another function as an argument and returns a When<T>.

When<T> Function<T>(T Function() x) get when { ... }
\_________________________________/  ^   ^
                |                    |   |
                |                    |    -- Name of the member
                |                    |
                |                     ------ Member is a getter
                |
                 --------------------------- Return type of the getter
  •  Tags:  
  • dart
  • Related