ListItemBuilder is defined as a function that returns Widget when PageResult and page index are given.

typedef Widget ListItemBuilder<P extends PageResult>(P pageData, int index);

I created QueryPageResult by extending PageResult class.

class QueryPageResult<T> extends PageResult {
  List<T> result;
}

In a Paginator created by me used ListItemBuilder as follows.

class Paginator<P extends PageResult> extends StatefulWidget {
   final ListItemBuilder<P> itemBuilder;
}

When debugging the app at runtime, it shows type of the widget.itemBuilder as

(QueryPageResult<ResolutionCase>, int) => XCardView

But when I try to call widget.itemBuilder(pageData, itemIndex) it gives me,

Unhandled exception:
type '(QueryPageResult<ResolutionCase>, int) => XCardView' is not a subtype of type '(PageResult, int) => Widget'

Why am I getting this exception at runtime?


Solution 1: spkersten

The code below works. Maybe that helps you to identify if you missing something.

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  Container foo(QueryPageResult<int> result, int index) => Container(color: Color(0xff00ff00));

  @override
  Widget build(BuildContext context) {
    return Paginator<QueryPageResult<int>>(
      itemBuilder: foo,
      data: QueryPageResult<int>(),
    );
  }
}

class PageResult {}

class QueryPageResult<T> extends PageResult {
  List<T> result;
}

class Paginator<P extends PageResult> extends StatefulWidget {
  const Paginator({Key key, @required this.itemBuilder, @required this.data}) : super(key: key);

  final P data;
  final ListItemBuilder<P> itemBuilder;

  @override
  State<StatefulWidget> createState() => PaginatorState<P>();
}

class PaginatorState<P extends PageResult> extends State<Paginator<P>> {
  @override
  Widget build(BuildContext context) {
    return widget.itemBuilder(widget.data, 1);
  }
}

typedef Widget ListItemBuilder<P extends PageResult>(P pageData, int index);