未处理的异常:NoSuchMethodError:在空调用getter'focusScopeNode'

时间:2020-06-01 08:49:40

标签: flutter dart

我是新手,对“ focusScopeNode”一直遇到问题。我也在该网站上尝试了其他解决方案,但无法检测到该问题。我无法理解上下文和构建器的概念。任何帮助将不胜感激。

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

class Homepage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      routes: {
        '/login': (context) => Login(),
      },
      title: 'NavigationDrawer Demo',
      theme: new ThemeData(
        primarySwatch: Colors.red,
      ),
      home: new HomePage(),
    );
  }
}
class DrawerItem {
  String title;
  IconData icon;
  DrawerItem(this.title, this.icon);
}
FirebaseUser user;

class HomePage extends StatefulWidget {
  HomePage({
    this.auth,
    this.onSignedOut,
  });
  final AuthImplementation auth;
  final VoidCallback onSignedOut;
  final drawerItems = [
    new DrawerItem("Home", Icons.home),
    new DrawerItem("Logout", Icons.exit_to_app),
  ];

  @override
  State<StatefulWidget> createState() {
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  void _logOutUser() async {
    try {
      await widget.auth.signOut();
      widget.onSignedOut();

    } catch (e) {
      print(e.toString());
    }
  }

  int _selectedDrawerIndex = 0;
  _getDrawerItemWidget(int pos) {
    switch (pos) {
      case 0:
        return new MyTabbedPage();
      case 7:
        _logOutUser();
        return new StartApp();
      default:
        return new Text("Error");
    }
  }

  _onSelectItem(int index) {
    setState(() => _selectedDrawerIndex = index);
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    var drawerOptions = <Widget>[];
    for (var i = 0; i < widget.drawerItems.length; i++) {
      var d = widget.drawerItems[i];
      drawerOptions.add(
          new ListTile(
            leading: new Icon(d.icon),
            title: new Text(d.title),
            selected: i == _selectedDrawerIndex,
            onTap: () => _onSelectItem(i),
          )
      );
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我认为问题可能出在您调用_onSelectedItem()方法的地方。尝试将方法放在Build上下文中,如下所示:

@override
  Widget build(BuildContext context) {

_onSelectItem(int index) {
    setState(() => _selectedDrawerIndex = index);
    Navigator.of(context).pop();
  }

    var drawerOptions = <Widget>[];
    for (var i = 0; i < widget.drawerItems.length; i++) {
      var d = widget.drawerItems[i];
      drawerOptions.add(
          new ListTile(
            leading: new Icon(d.icon),
            title: new Text(d.title),
            selected: i == _selectedDrawerIndex,
            onTap: () => _onSelectItem(i),
          )
      );
    }
  }

可能是当您调用Navigator.pop(context)时,它不知道“上下文”是什么。因此,通过将方法放在Build(context)中,它应该可以解决它。