Dart: generic type extending a class with generic types in it

206

You see the error because the compiler doesn't know where the generic type comes from and thinks it's a concrete class (such as int, double, String...); however, it's not.

You can achieve it specifying U and V as type parameters to QuestionPageBaseState which will pass them to Question:

abstract class QuestionPageBaseState<T extends Question<U, V>, U, V> {}
Share:
206
Quentin
Author by

Quentin

Updated on December 26, 2022

Comments

  • Quentin
    Quentin over 1 year

    I have a class defined like this:

    abstract class QuestionPageBaseState<T extends Question<dynamic, dynamic>> {...

    The class Question is defined like this:

    abstract class Question<T, U> {...

    is it possible to replace the dynamic with a generic type like this?:

    abstract class QuestionPageBaseState<T extends Question<U, V>> {...

    When I try I get this error: The name 'U' isn't a type so it can't be used as a type argument.

  • Quentin
    Quentin over 2 years
    Amazing, exactly what I was looking for! I couldn't find any documentation with such a case, do you know if there is any doc somewhere?
  • Stewie Griffin
    Stewie Griffin over 2 years
    I'm not sure if there is any documentation, but there are a few issues regarding this on github. You may find some useful examples provided by Dart team.