Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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,
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading