Let me introduce the errors why I got this issue. (Detail here)

type '(String, String) => bool' is not a subtype of type '(dynamic, String) => bool'

This is the error from material_search, and the solution is:

   - _filter(T v, String c) {
   + _filter(dynamic v, String c) {
   - onSelect: (T value) => Navigator.of(context).pop(value),
   + OnSelect: (dynamic value) => Navigator.of(context).pop(value),

Changing all of generics type T to dynamic, and The issue seems to be happened when dart 2 is appearing.

So, I got these question here,

  1. What is the difference between generics and dynamic in dart?
  2. What's the limit that only works with generics or on the other hand? In the above issue, this is only works with dynamic.

Edit: Let me provide a simple example to make question more clear:

Define a class with genetic

typedef bool GeneticFunction<T>(T geneticData, String key);

class Component<T> extends StatefulWidget {
  final GeneticFunction<T> geneticFunc;

  _componentState<T> createState() => _componentState<T>();

One of the method is working below

      geneticFunc: (CoolType cool, String keyword){
        return false;
      geneticFunc: (dynamic cool, String keyword){
        return false;

The answer #2 is working, and It means I don't even need generic, just go dynamic. If you use #1, there is sometimes even no error in runtime, and you might stuck at there whole day.

There is an official discussion here, said that T would always be dynamic in runtime, so #2 is the only choosen.

In the conclusion, I have no idea when to use generic, and seems to be always use dynamic now, because of the result above.

0 Answers