如何解决错误“ NoSuchMethodError:将吸气剂“ focusScopeNode”调用为null”? [英] How to fix the error 'NoSuchMethodError: The getter 'focusScopeNode' was called on null'?

查看:180
本文介绍了如何解决错误“ NoSuchMethodError:将吸气剂“ focusScopeNode”调用为null”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Navigator类路由到另一个屏幕,但是当单击按钮路由到下一个屏幕时,什么也没有发生,并且在控制台中,我得到了错误。

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.


未处理的异常:NoSuchMethodError:在空调用getter'focusScopeNode'。
E / flutter(18828):接收方:空
E / flutter(18828):尝试调用:focusScopeNode

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

我无法理解 Context builders 的概念。

这是我的 main.dart 文件(第一个屏幕)

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);
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是我的收藏品。 dart 文件(SecondScreen)

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")),
    );
  }
}

我希望它可以路由到第二个屏幕collections.dart文件,但没有任何反应。
在控制台中:

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):


推荐答案

请删除Navigator.pop(context);

Please remove Navigator.pop(context);

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

完整代码

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")),
    );
  }
}

这篇关于如何解决错误“ NoSuchMethodError:将吸气剂“ focusScopeNode”调用为null”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆