简体   繁体   中英

Too many positional arguments error when trying to use openDrawer() using Scaffold.of(context) finding 2 instead of 0

class MyApp extends StatelessWidget {
  @override

  Widget build(BuildContext context) {

   return MaterialApp(

        home: SafeArea(

            child: Scaffold(

      Builder(

          builder: (context) => AppBar(

                leading: IconButton(

                  icon: Icon(Icons.accessible),

                  onPressed: () => Scaffold.of(context).openDrawer(),

                ),
                title: Text('Sorted.'),
                backgroundColor: Color(0xff0A3D62),
              )),

      Drawer(

          child: ListView(padding: EdgeInsets.zero, children: <Widget>[

        new UserAccountsDrawerHeader(

          accountName: new Text('XYZ'),
          accountEmail: new Text('XYZ@gmail.com'),
          currentAccountPicture: new CircleAvatar(),
        )
      ])),

      body: Center(child: Home()),)

    ));
  }
}` 

Error: Too many positional arguments: 0 expected, but 2 found. Try removing the extra positional arguments, or specifying the name for named arguments.

Please help me resolve this. Thanks in advance!

This is because you cannot pass AppBar and Drawer parameters to Scaffold like this..

Scaffold(
    AppBar(),
    Drawer(),
)

..because these are named parameters. Instead, you need to pass them with the corresponding parameter names like..

Scaffold(
    appBar: AppBar(),
    drawer: Drawer(),
)
return MaterialApp(
    home: SafeArea(
        child: Scaffold(
  appBar: AppBar(
    leading: IconButton(
      icon: Icon(Icons.accessible),
      onPressed: () => Scaffold.of(context).openDrawer(),
    ),
    title: Text('Sorted.'),
    backgroundColor: Color(0xff0A3D62),
  ),
  drawer: Drawer(
      child: ListView(padding: EdgeInsets.zero, children: <Widget>[
    new UserAccountsDrawerHeader(
      accountName: new Text('XYZ'),
      accountEmail: new Text('XYZ@gmail.com'),
      currentAccountPicture: new CircleAvatar(),
    )
  ])),
  body: Center(child: Home()),
)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM