Flutter-NoSuchMethodError:在null上调用了吸气剂“ focusScopeNode”

时间:2020-01-18 15:28:24

标签: flutter statefulwidget

尝试将List从一个屏幕传递到另一个屏幕时出现以下错误。数据是从async函数传递的。我需要根据从数据库中获取的数据打开另一个屏幕。 here中提到的答案对我不起作用。 这是错误。

: [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(频道稳定,v1.12.13 + hotfix.5,在Linux上,语言环境为en_US.UTF-8)
•位于/home/jamshaid/Documents/flutter_linux_v1.9.1+hotfix.4-stable/flutter的Flutter 1.12.13 + hotfix.5版本
•Framework修订版27321ebbad(6周前),2019-12-10 18:15:01 -0800 •发动机版本2994f7e1e6
•Dart版本2.7.0
完整的同步功能

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

异步呼叫者代码 onPressed的{​​{1}}中正在调用异步功能。

RaisedButton

1 个答案:

答案 0 :(得分:4)

在切换块后面还有另一个Navigator.pop(context);。如果意图是在所有案例都不匹配的情况下向后浏览,则也尝试删除该案例或设置一个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);
  }