gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:33:58 25 4
gpt4 key购买 nike

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

Unhandled Exception: NoSuchMethodError: The getter 'focusScopeNode' was called on null. E/flutter (18828): Receiver: null E/flutter (18828): Tried calling: 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),
)
);
}
}
}

最佳答案

我认为问题可能出在您调用_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)中,它应该可以解决它。

关于flutter - 未处理的异常:NoSuchMethodError:在空调用getter 'focusScopeNode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62128614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com