Search code examples
functionflutterdartreturn-type

How to indicate the return type of a function variable


I have declared function variable like this;

final Function localVideo;

Is there a way to indicate that this function has a return type of Widget?

Current code based on answers are;

typedef LocalVideoReturnWidget = Widget Function();

class VideoLocalOverlay extends StatefulWidget {

  final LocalVideoReturnWidget localVideo;

const VideoLocalOverlay(
      {Key key,
      this.localVideo,...

This gives error

type '() => dynamic' is not a subtype of type '() => Widget'

Plan to use the function as

child: GestureDetector(
                    onTap: () {
                      print("Click");
                    },
                    child: widget.localVideo(),
                    ),

The way I have defined before works fine without any error. But for some reason I can not get the GestureDetector working. I thought it may be due to the fact return type is not clear.


Solution

  • Add this on top of file

    typedef int FunctionThatReturnInt(inputInt number);

    Another way of using typedef

    typedef FunctionThatReturnInt = int Function(inputInt number);

    and use the function like

    final FunctionThatReturnInt localVideo;

    inside the class or wherever you are using