Correctly trigger Function with key in Flutter

So I want to trigger opening a drawer in Flutter

My site looks like this:

final GlobalKey<ScaffoldState> _key = GlobalKey(); // Create a key

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        key: _key,
        drawer: HomeDrawer(),
        body: StartAppBar(_key.currentState!.openDrawer),
        bottomNavigationBar: BottomBar(),
      ),
    );
  }
}

In this line I try to reference the function:

body: StartAppBar(_key.currentState!.openDrawer),

& in my StartAppBar I wrote:

class StartAppBar extends StatelessWidget {
    void Function() openDrawer;
    StartAppBar(this.openDrawer);

& on onPressed in the StartAppBar Im trying to call the function

onPressed: () {
            openDrawer;
          },

But it somehow says that its an unnecessary statement, so I cant open the drawer. What am I doing wrong?

>Solution :

you forgot parenthesis so it call the function:

onPressed: () {
    openDrawer();
},

or simply

onPressed: openDrawer,

Leave a Reply