Flutter - NoSuchMethodError: The getter 'focusScopeNode' was called on null
Flutter - NoSuchMethodError: The getter 'focusScopeNode' was called on null
我在尝试将 List
从一个屏幕传递到另一个屏幕时遇到以下错误。数据正在从 async
函数传递。我需要根据从数据库中获取的数据打开另一个屏幕。
提到的答案 对我不起作用。
这是错误。
: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The
getter 'focusScopeNode' was called on null.
E/flutter (26425): Receiver: null
E/flutter (26425): Tried calling: focusScopeNode
E/flutter (26425): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (26425): #1 Route.didPush.<anonymous closure> (package:flutter/src/widgets/navigator.dart:139:17)
异步函数中的代码
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
家长主屏幕
class ParentHome extends StatefulWidget {
List childDataList;
ParentHome(this.childDataList);
@override
State<StatefulWidget> createState() {
return ParentHomeWidget();
}
}
class ParentHomeWidget extends State<ParentHome> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
body: Container(
margin: EdgeInsets.all(5),
child: ListView.builder(
itemCount: widget.childDataList.length,
itemBuilder: (BuildContext context, int index) => ListTile(
title: Text(widget.childDataList[index]['title']),
)),
),
),
);
}
}
我没有找到任何关于此的帮助。请帮我解决这个问题。我还是 Flutter 的新手。谢谢
Flutter Doctor 输出
Flutter (Channel stable, v1.12.13+hotfix.5, on Linux, locale en_US.UTF-8)
• Flutter version 1.12.13+hotfix.5 at /home/jamshaid/Documents/flutter_linux_v1.9.1+hotfix.4-stable/flutter
• Framework revision 27321ebbad (6 weeks ago), 2019-12-10 18:15:01 -0800
• Engine revision 2994f7e1e6
• Dart version 2.7.0
complete sync function
Future<void> requestLogin(String userType) async {
/*
final response =
await http.get('http://10.0.2.2/php_to_flutter/login_validation.php');*/
String databaseAddress = "";
switch (userType) {
case "Student":
databaseAddress = AppUtils.studentLoginLink;
break;
case "Parent":
databaseAddress = AppUtils.parentLoginLink;
break;
case "Teacher":
databaseAddress = AppUtils.teacherLoginLink;
break;
}
final response = await http
.post(databaseAddress, body: {"email": email, "password": password});
String responseBody = response.body;
if (response.statusCode == 200) {
switch (userType) {
case "Student":
print("Registration # $responseBody");
break;
case "Parent":
List childList = responseBody.split(",");
//child list needs to be separated by :
/*Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ParentHome(childList)));
*/
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
}));
//Navigator.pop(context);
print("Child List= $responseBody");
break;
case "Teacher":
print("Teacher id= $responseBody");
break;
}
form_key.currentState.reset();
//dismissing progress dialog
Navigator.pop(context);
Toast.show("Login Successful", context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
} else {
Navigator.pop(context);
print(databaseAddress);
Toast.show("Error: " + response.statusCode.toString(), context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
print('error' + response.body);
print(response.statusCode);
}
}
异步调用代码
正在 RaisedButton
的 onPressed
中调用异步函数。
Widget studentLoginButton() {
return RaisedButton(
onPressed: () {
if (form_key.currentState.validate()) {
form_key.currentState.save();
// pr.show();
AppUtils.showProgressDialog(context, "Loggin in...");
requestLogin("Student");
form_key.currentState.reset();
//pr.dismiss();
}
// form_key.currentState.reset();
},
child: Text('S Login'),
color: Colors.lightBlue,
);
}
在开关块之后还有另一个 Navigator.pop(context);
。如果 none 个案例匹配,则也尝试删除该案例或设置 default
个案例。
switch (userType) {
case "Student":
print("Registration # $responseBody");
break;
case "Parent":
List childList = responseBody.split(",");
//child list needs to be separated by :
/*Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ParentHome(childList)));
*/
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
}));
//Navigator.pop(context);
print("Child List= $responseBody");
break;
case "Teacher":
print("Teacher id= $responseBody");
break;
}
form_key.currentState.reset();
//dismissing progress dialog
//Navigator.pop(context);
Toast.show("Login Successful", context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
或
switch (userType) {
case "Student":
print("Registration # $responseBody");
break;
case "Parent":
List childList = responseBody.split(",");
//child list needs to be separated by :
/*Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ParentHome(childList)));
*/
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
}));
//Navigator.pop(context);
print("Child List= $responseBody");
break;
case "Teacher":
print("Teacher id= $responseBody");
break;
default:
form_key.currentState.reset();
//dismissing progress dialog
Navigator.pop(context);
Toast.show("Login Successful", context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
}
我在尝试将 List
从一个屏幕传递到另一个屏幕时遇到以下错误。数据正在从 async
函数传递。我需要根据从数据库中获取的数据打开另一个屏幕。
提到的答案
: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The
getter 'focusScopeNode' was called on null.
E/flutter (26425): Receiver: null
E/flutter (26425): Tried calling: focusScopeNode
E/flutter (26425): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (26425): #1 Route.didPush.<anonymous closure> (package:flutter/src/widgets/navigator.dart:139:17)
异步函数中的代码
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
家长主屏幕
class ParentHome extends StatefulWidget {
List childDataList;
ParentHome(this.childDataList);
@override
State<StatefulWidget> createState() {
return ParentHomeWidget();
}
}
class ParentHomeWidget extends State<ParentHome> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
body: Container(
margin: EdgeInsets.all(5),
child: ListView.builder(
itemCount: widget.childDataList.length,
itemBuilder: (BuildContext context, int index) => ListTile(
title: Text(widget.childDataList[index]['title']),
)),
),
),
);
}
}
我没有找到任何关于此的帮助。请帮我解决这个问题。我还是 Flutter 的新手。谢谢
Flutter Doctor 输出
Flutter (Channel stable, v1.12.13+hotfix.5, on Linux, locale en_US.UTF-8)
• Flutter version 1.12.13+hotfix.5 at /home/jamshaid/Documents/flutter_linux_v1.9.1+hotfix.4-stable/flutter
• Framework revision 27321ebbad (6 weeks ago), 2019-12-10 18:15:01 -0800 • Engine revision 2994f7e1e6
• Dart version 2.7.0
complete sync function
Future<void> requestLogin(String userType) async {
/*
final response =
await http.get('http://10.0.2.2/php_to_flutter/login_validation.php');*/
String databaseAddress = "";
switch (userType) {
case "Student":
databaseAddress = AppUtils.studentLoginLink;
break;
case "Parent":
databaseAddress = AppUtils.parentLoginLink;
break;
case "Teacher":
databaseAddress = AppUtils.teacherLoginLink;
break;
}
final response = await http
.post(databaseAddress, body: {"email": email, "password": password});
String responseBody = response.body;
if (response.statusCode == 200) {
switch (userType) {
case "Student":
print("Registration # $responseBody");
break;
case "Parent":
List childList = responseBody.split(",");
//child list needs to be separated by :
/*Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ParentHome(childList)));
*/
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
}));
//Navigator.pop(context);
print("Child List= $responseBody");
break;
case "Teacher":
print("Teacher id= $responseBody");
break;
}
form_key.currentState.reset();
//dismissing progress dialog
Navigator.pop(context);
Toast.show("Login Successful", context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
} else {
Navigator.pop(context);
print(databaseAddress);
Toast.show("Error: " + response.statusCode.toString(), context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
print('error' + response.body);
print(response.statusCode);
}
}
异步调用代码
正在 RaisedButton
的 onPressed
中调用异步函数。
Widget studentLoginButton() {
return RaisedButton(
onPressed: () {
if (form_key.currentState.validate()) {
form_key.currentState.save();
// pr.show();
AppUtils.showProgressDialog(context, "Loggin in...");
requestLogin("Student");
form_key.currentState.reset();
//pr.dismiss();
}
// form_key.currentState.reset();
},
child: Text('S Login'),
color: Colors.lightBlue,
);
}
在开关块之后还有另一个 Navigator.pop(context);
。如果 none 个案例匹配,则也尝试删除该案例或设置 default
个案例。
switch (userType) {
case "Student":
print("Registration # $responseBody");
break;
case "Parent":
List childList = responseBody.split(",");
//child list needs to be separated by :
/*Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ParentHome(childList)));
*/
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
}));
//Navigator.pop(context);
print("Child List= $responseBody");
break;
case "Teacher":
print("Teacher id= $responseBody");
break;
}
form_key.currentState.reset();
//dismissing progress dialog
//Navigator.pop(context);
Toast.show("Login Successful", context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
或
switch (userType) {
case "Student":
print("Registration # $responseBody");
break;
case "Parent":
List childList = responseBody.split(",");
//child list needs to be separated by :
/*Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ParentHome(childList)));
*/
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ParentHome(childList);
}));
//Navigator.pop(context);
print("Child List= $responseBody");
break;
case "Teacher":
print("Teacher id= $responseBody");
break;
default:
form_key.currentState.reset();
//dismissing progress dialog
Navigator.pop(context);
Toast.show("Login Successful", context,
duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
}