Flutter/Dart: The argument type 'bool Function(MyStateNotifier)' can't be assigned to the parameter type 'dynamic Function(dynamic)'

1,484

Solution 1

Thank aligator for your reply, but actually it's analyzer problem. After restart VSCode, the problem gone :-)

Solution 2

There is 2 main problems with your code:

  1. You should name the input of the function, for example something like notifier.
typedef ContextConditionFilter<T, V> = V Function(T notifier);
  1. You don't need return when using => when defining a function:
class MyStateNotifier extends StateNotifier<DrawMenuState> {
   bool get myValue => true;
}

Everything else should work if you fix these problems.

You also don't need parentheses around your notifier function.

ContextStateWidget<MyStateNotifier>(
    filter: (MyStateNotifier notifier) => notifier.myValue,
    child: const OtherWidget(),
);
Share:
1,484
hoaquo
Author by

hoaquo

Updated on December 23, 2022

Comments

  • hoaquo
    hoaquo over 1 year

    I have a such class declaration in Dart, but when using it the compiler reports error

    typedef ContextConditionFilter<T, bool> = bool Function(T);
    
    class ContextStateWidget<T extends StateNotifier> extends StatefulWidget
    {
        final Widget child;
        final ContextConditionFilter<T, bool> filter;
        const ContextStateWidget({Key key, @required this.child, @required this.filter}) : super(key: key);
    }
    

    A class inherited StateNotifier

    class MyStateNotifier extends StateNotifier<DrawMenuState>
    {
    ...
    bool get myValue => return true;
    }
    

    Using it

    ContextStateWidget<MyStateNotifier>(
        filter: ((MyStateNotifier notifier) => notifier.myValue), // error here
        child: const OtherWidget()
    );
    

    Error

    The argument type 'bool Function(MyStateNotifier)' can't be assigned to the parameter type 'dynamic Function(dynamic)'
    
    • lenz
      lenz over 3 years
      Why not accept the answer? or comment on it?