我正在尝试使用Navigator类路由到另一个屏幕,但是当单击按钮路由到下一个屏幕时,什么也没有发生,并且在控制台中,我得到了错误。
未处理的异常:NoSuchMethodError:在null上调用了getter'focusScopeNode'。 E / flutter(18828):接收者:null E / flutter(18828):尝试调用: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
文件(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文件,但没有任何反应。 在控制台中:
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):
答案 0 :(得分:1)
请删除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")),
);
}
}
答案 1 :(得分:0)
尝试删除.dart_tool / build目录,然后重新构建作为解决方法。
也许是这项工作