Home > OS >  How to open EndDrawer
How to open EndDrawer

Time:12-02

I'm trying to adapt flutter source code to create a side panel. My code is this :



class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  void _openEndDrawer(){
    _scaffoldKey.currentState!.openEndDrawer();
  }
  void _closeEndDrawer(){
    Navigator.of(context).pop();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HEADER(appBar:new AppBar()),
      drawer:LeftPanel(drawer: new Drawer()),
      endDrawer:  Drawer(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text('This is the Drawer'),
              ElevatedButton(
                onPressed: _closeEndDrawer,
                child: const Text('Close Drawer'),
     ...
      body: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SectionTitle(title: "Historial"),
              Padding(
                padding: EdgeInsets.fromLTRB(10, 30, 0, 0),
                child: IconButton (
                  icon: Icon(Icons.filter_alt),
                  onPressed: _openEndDrawer,

......

when i click on the button flutter throws this error :

======== Exception caught by gesture ===============================================================
The following _CastError was thrown while handling a gesture:
Null check operator used on a null value

At this point I don't know what to do ..., Im new codding with flutter

CodePudding user response:

You have missed to attach the scaffold key to the scaffold. Due to that accessing currentState giving the error. so make sure to attach the key using key: _scaffoldKey

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(title: const Text('Drawer Demo')),
      body: (..)
    );
  }

CodePudding user response:

You got an error because your Scaffold key is null

Scaffold( key: _scaffoldKey,...
  • Related