flutterdartandroid-alertdialog

Error: "The argument type 'Context' can't be assigned to the parameter type 'BuildContext'"


I am creating an Alert dialog widget, but it is giving this error:

The argument type 'Context' can't be assigned to the parameter type 'BuildContext'.

CODE:

IconButton _iconButton() => IconButton(
      onPressed: () {
        showDialog(
          context: context, // ====>>>> error is shown here
          builder: (context) => AlertDialog(
            title: AppBar(
              title: Text("are you sure you want to delete"),
            ),
            content: Text(""),
            actions: [
              TextButton.icon(
                  onPressed: () {},
                  icon: Icon(Icons.playlist_add_check_circle_rounded),
                  label: Text("YES")),
              TextButton.icon(
                  onPressed: () {},
                  icon: Icon(Icons.cancel),
                  label: Text("CANCEL"))
            ],
          ),
        );
      },
      icon: Icon(Icons.ads_click));

Solution

  • If you intend to use _iconButton as a function, do this:

    IconButton _iconButton(BuildContext context) => IconButton( // ...
    

    If instead you want to use it as a simple widget do this:

    IconButton _iconButton = IconButton( // ...