instanceMemberGet method

  1. @override
dynamic instanceMemberGet(
  1. dynamic instance,
  2. String id, {
  3. bool ignoreUndefined = false,
})
override

Default HTExternalClass constructor. Fetch a instance member of the Dart class by the id, in the form of

object.key

Implementation

@override
dynamic instanceMemberGet(dynamic instance, String id,
    {bool ignoreUndefined = false}) {
  switch (id) {
    case 'nextBool':
      return ({object, positionalArgs, namedArgs}) => object.nextBool();
    case 'nextBoolBiased':
      return ({object, positionalArgs, namedArgs}) {
        final double input = (positionalArgs[0] as num).toDouble();
        final double target = (positionalArgs[1] as num).toDouble();
        return (object as math.Random).nextBoolBiased(input, target);
      };
    case 'nextDouble':
      return ({object, positionalArgs, namedArgs}) => object.nextDouble();
    case 'nearInt':
      return ({object, positionalArgs, namedArgs}) {
        return (object as math.Random)
            .nearInt(positionalArgs.first, bias: namedArgs['bias']);
      };
    case 'distantInt':
      return ({object, positionalArgs, namedArgs}) => (object as math.Random)
          .distantInt(positionalArgs.first, bias: namedArgs['bias']);
    case 'nextInt':
      return ({object, positionalArgs, namedArgs}) =>
          object.nextInt(positionalArgs[0].toInt());
    case 'nextColorHex':
      return ({object, positionalArgs, namedArgs}) =>
          (object as math.Random).nextColorHex(
            hasAlpha: namedArgs['hasAlpha'],
          );
    case 'nextBrightColorHex':
      return ({object, positionalArgs, namedArgs}) =>
          (object as math.Random).nextBrightColorHex(
            hasAlpha: namedArgs['hasAlpha'],
          );
    case 'nextIterable':
      return ({object, positionalArgs, namedArgs}) =>
          (object as math.Random).nextIterable(positionalArgs.first);
    case 'shuffle':
      return ({object, positionalArgs, namedArgs}) sync* {
        final Iterable iterable = positionalArgs.first;
        if (iterable.isNotEmpty) {
          // ignore: prefer_collection_literals
          final Set indexes = LinkedHashSet();
          int index;
          do {
            do {
              index = object.nextInt(iterable.length);
            } while (indexes.contains(index));
            indexes.add(index);
            yield iterable.elementAt(index);
          } while (indexes.length < iterable.length);
        }
      };
    default:
      throw HTError.undefined(id);
  }
}