Contact extends DeletableItem, and is a subtype of it.
Functions are contravariant in their parameters, so
Widget Function(Contact)
is not a subtype of
Widget Function(DeletableItem)
In fact, it's the other way around.
Functions act that way because subtyping means substitutability. You can use a Contact anywhere a DeletableItem is needed (because it implements the entire DeletableItem interface), so Contact is a subtype of DeletableItem.
You can use a Widget Function(DeletableItem) anywhere a Widget Function(Contact) is needed, because you can call that function with any Contact (and it returns a Widget as expected). So Widget Function(DeletableItem) is a subtype of Widget Function(Contact).
Dart generics are covariant, even though it's not always sound.
That means that a UndoableListView<Contact> is a subtype of UndoableListView<DeletableItem>.
The problem comes up because the cardBuilder field uses the class type parameter contravariantly in the function type,
final Widget Function(T item) cardBuilder;
So, when you do:
UndoableListView<DeletableItem> list = UndoableListView<Contact>(); var builder = list.cardBuilder;
you get an error. The list.cardBuilder expression has a static type of Widget Function(DeletableItem), but a run-time type of Widget Function(Contact), which is not a subtype of the static type. That's a type soundness problem, one the compiler is aware of, and it inserts a type check to ensure that the builder variable won't end up with an invalidly typed value.
And that check throws.
Contactdoesn't implementDeletableItem, but rather that the whole function itself has a different signature since(Contact) => Widgetcannot be cast to(DeletableItem) => Widget. Without seeing more code, it's hard to propose a fix. Can you show the context in whichbuildItemis being used?Contactis not a subtype ofDeletableItem, but that the function typeWidget Function(Contact)is not a subtype of the function typeWidget Function(DeletableItem). Function types are contravariant in their parameter types (a function type can only a subtype of another function type if the parameter types of the first function are supertypes of the parameter types of the second function). So, this is working as intended.