Flutter make a type callable

Viewed 37

I would like to know if exists the possibility to make a type callable.

I mean, I have this class:

class MyFutureBuilder<T> extends StatefulWidget {
  const MyFutureBuilder({
    required this.incomingWidget,
    required this.futureCallback,
    Key? key,
  }) : super(key: key);
  final T incomingWidget;
  final Future<T> futureCallback;

  @override
  State<MyFutureBuilder<T>> createState() =>
      _MyFutureBuilderState<T>();
}

class _MyFutureBuilderState<T> extends State<MyFutureBuilder<T>> {
  @override
  Widget build(BuildContext context) => FutureBuilder<T>(
        future: Future.any([widget.futureCallback]),
        builder: (context, AsyncSnapshot<T> snapshot) {
          if (!snapshot.hasData) {
            return CircularProgressIndicator();
          } else {
            return widget.incomingWidget(snapshot.data);
          }
        },
      );
}

I want to have a generic future builder that receives a Widget and a Future. But, I need to delay the initialization of the Widget until the future completes and I have the snapshot.data so I can initialize my Widget with that data.

How can I achieve this?

Thanks.

0 Answers
Related