gpt4 book ai didi

android - 如何修复错误 'NoSuchMethodError: The getter ' focusScopeNode' was called on null'?

转载 作者:行者123 更新时间:2023-12-03 02:42:18 28 4
gpt4 key购买 nike

我正在尝试使用 Navigator 类路由到另一个屏幕,但是当单击按钮路由到下一个屏幕时,没有任何 react ,并且在控制台中,我收到了错误消息。

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文件(第一个屏幕)
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);
},
),
],
),
),
),
),
);
}
}

这是我的 collections.dart文件(第二屏)
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 文件,但什么也没发生。
在控制台中:
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);

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

enter image description here

关于android - 如何修复错误 'NoSuchMethodError: The getter ' focusScopeNode' was called on null'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58498679/

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