Thierry
Thierry

Reputation: 8393

Freezed class with generic callback

I would like to define a freezed class [https://pub.dev/packages/freezed] with a generic callback.

Freezed class:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'foo.freezed.dart';

@freezed
abstract class Foo<T> with _$Foo {
  factory Foo({
    // String Function() callBackOne,
    String Function(T) callBackTwo,
  }) = _Foo;
}

Widget using the Freezed class:

class MyHomePage extends StatelessWidget {
  // final fooOne = Foo<int>(callBackOne: () => 'Result: 42');
  final fooTwo = Foo<int>(callBackTwo: (value) => 'Result: ${value * 3}');
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(fooTwo.callBackTwo(14)),
      ),
    );
  }
}

Error:

lib/foo.freezed.dart:128:26: Error: The return type of the method '_Foo.callBackTwo' is 'String Function(T)', which does not match the return type, 'String Function(dynamic)', of the overridden method, '_$Foo.callBackTwo'.
Change to a subtype of 'String Function(dynamic)'.
  String Function(T) get callBackTwo;
                         ^
lib/foo.freezed.dart:31:26: Context: This is the overridden method ('callBackTwo').
  String Function(T) get callBackTwo;

                         ^

Do you know what is wrong with my code? Is it a limitation of Freezed? Do you know a workaround?

Thank you!!!

Upvotes: 5

Views: 3025

Answers (1)

ChessMax
ChessMax

Reputation: 2265

It looks like a flaw in Dart type system. I've encouraged something like that either. I don't know a clean workaround. You can specify not a direct function but a function wrapped into a class with a "strong" method signature. Something like that should work:

@freezed
abstract class Foo<T> with _$Foo {
  factory Foo({
    Func<T> callBackTwo,
  }) = _Foo;
}

class Func<T> {
  final String Function(T) _apply;

  Func(this._apply) : assert(_apply != null);

  String call(T value) {
    return _apply(value);
  }
}

class MyHomePage extends StatelessWidget {    
  final fooTwo = Foo<int>(Func<int>((value) => 'Result: ${value * 3}'));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(fooTwo.callBackTwo(14)),
      ),
    );
  }
}

It's not so good, because you are to type more. But we can minimize typing a bit:

@freezed
abstract class Foo<T> with _$Foo {
  factory Foo({
    Func<T> callBackTwo,
  }) = _Foo;

  factory Foo.from(String Function(T) arg) {
    return Foo<T>(callBackTwo: Func<T>(arg));
  }
}

class MyHomePage extends StatelessWidget {    
  final fooTwo = Foo<int>.from((value) => 'Result: ${value * 3}');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(fooTwo.callBackTwo(14)),
      ),
    );
  }
}

Upvotes: 3

Related Questions