How to fix the error 'NoSuchMethodError: The getter 'focusScopeNode' was called on null'?

8,313

Please remove Navigator.pop(context);

ListTile(
            title: Text('Collections'),
            leading: Icon(Icons.monetization_on),
            onTap: () {
              Navigator.of(context).pushNamed('/collections');
              //Navigator.pop(context);
            },
          ),

full code

import 'package:flutter/material.dart';
void main() => runApp(Bsk());
class Bsk extends StatelessWidget {
  static final GlobalKey<ScaffoldState> scaffoldKey =
  GlobalKey<ScaffoldState>();
  @override
  Widget build(BuildContext context) {
    final appTitle = 'BSK Management';
    return MaterialApp(
      routes: {
        '/home': (context) => Bsk(),
        '/collections': (context) => Collections(),
      },
      title: appTitle,
      home: Builder(builder: (context) => Scaffold(
        //resizeToAvoidBottomInset: false,
        key:scaffoldKey,
        appBar: AppBar(
          title: Text(appTitle),
        ),
        body: Text('hi'),
        drawer: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              DrawerHeader(
                child: Text('BSK Management'),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
              ListTile(
                title: Text('Home'),
                leading: Icon(Icons.home),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: Text('Collections'),
                leading: Icon(Icons.monetization_on),
                onTap: () {
                  Navigator.of(context).pushNamed('/collections');
                  //Navigator.pop(context);
                },
              ),
            ],
          ),
        ),
      ),
      ),
    );
  }
}
class Collections extends StatefulWidget {
  @override
  _CollectionsState createState() => _CollectionsState();
}
class _CollectionsState extends State<Collections> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(child: Text("Hello")),
    );
  }
}

enter image description here

Share:
8,313
Author by

Soumodeep Bhowmick

Updated on December 14, 2022

Comments

  • Soumodeep Bhowmick 14 minutes

    I am trying to route to another screen using the Navigator class, but when clicking on the button to route to the next screen nothing happens and in the console, I get the error.

    Unhandled Exception: NoSuchMethodError: The getter 'focusScopeNode' was called on null. E/flutter (18828): Receiver: null E/flutter (18828): Tried calling: focusScopeNode

    I am not able to understand the concept of Context and builders.

    This my main.dart file(First Screen)

    import 'package:flutter/material.dart';
    import 'adduser.dart';
    import 'collections.dart';
    void main() => runApp(Bsk());
    class Bsk extends StatelessWidget {
      static final GlobalKey<ScaffoldState> scaffoldKey = 
    GlobalKey<ScaffoldState>();
      @override
      Widget build(BuildContext context) {
        final appTitle = 'BSK Management';
        return MaterialApp(
          routes: {
               '/home': (context) => Bsk(),
               '/collections': (context) => Collections(),
          },
          title: appTitle,
          home: Builder(builder: (context) => Scaffold(
              //resizeToAvoidBottomInset: false,
              key:scaffoldKey,
              appBar: AppBar(
                title: Text(appTitle),
              ),
              body: SignUpPage(),
              drawer: Drawer(
                child: ListView(  
                  padding: EdgeInsets.zero,
                  children: <Widget>[
                    DrawerHeader(
                      child: Text('BSK Management'),
                      decoration: BoxDecoration(
                        color: Colors.blue,
                      ),
                    ),
                    ListTile(
                      title: Text('Home'),
                      leading: Icon(Icons.home),
                      onTap: () {
                        Navigator.pop(context);
                      },
                    ),
                    ListTile(
                      title: Text('Collections'),
                      leading: Icon(Icons.monetization_on),
                      onTap: () {
                        Navigator.of(context).pushNamed('/collections');
                        Navigator.pop(context);
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    

    This is my collections.dart file(SecondScreen)

    import 'package:flutter/material.dart';
    import 'main.dart';
    import 'adduser.dart';
    class Collections extends StatefulWidget {
      @override
      _CollectionsState createState() => _CollectionsState();
    }
    class _CollectionsState extends State<Collections> {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Center(child: Text("Hello")),
        );
      }
    }
    

    I expected it to route to the second screen that is collections.dart file but nothing is happening. In console:

    Restarted application in 1,421ms.
    E/flutter (18828): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled 
    Exception: NoSuchMethodError: The getter 'focusScopeNode' was called on 
    null.
    E/flutter (18828): Receiver: null
    E/flutter (18828): Tried calling: focusScopeNode
    E/flutter (18828): #0      Object.noSuchMethod (dart:core- 
    patch/object_patch.dart:51:5)
    E/flutter (18828): #1      Route.didPush.<anonymous closure> 
    (package:flutter/src/widgets/navigator.dart:139:17)
    E/flutter (18828): #2      _rootRunUnary (dart:async/zone.dart:1132:38)
    E/flutter (18828): #3      _CustomZone.runUnary 
    (dart:async/zone.dart:1029:19)
    E/flutter (18828): #4      _FutureListener.handleValue 
    (dart:async/future_impl.dart:137:18)
    E/flutter (18828): #5      
    Future._propagateToListeners.handleValueCallback 
    (dart:async/future_impl.dart:678:45)
    E/flutter (18828): #6      Future._propagateToListeners 
    (dart:async/future_impl.dart:707:32)
    E/flutter (18828): #7      Future._completeWithValue 
    (dart:async/future_impl.dart:522:5)
    E/flutter (18828): #8      Future._asyncComplete.<anonymous closure> 
    (dart:async/future_impl.dart:552:7)
    E/flutter (18828): #9      _rootRun (dart:async/zone.dart:1124:13)
    E/flutter (18828): #10     _CustomZone.run (dart:async/zone.dart:1021:19)
    E/flutter (18828): #11     _CustomZone.runGuarded 
    (dart:async/zone.dart:923:7)
    E/flutter (18828): #12     _CustomZone.bindCallbackGuarded.<anonymous 
    closure> (dart:async/zone.dart:963:23)
    E/flutter (18828): #13     _microtaskLoop 
    (dart:async/schedule_microtask.dart:41:21)
    E/flutter (18828): #14     _startMicrotaskLoop 
    (dart:async/schedule_microtask.dart:50:5)
    E/flutter (18828):