Flutter - NoSuchMethodError: The getter 'focusScopeNode' was called on null

帅比萌擦擦* 提交于 2020-04-16 05:55:10

问题


I am having the following error while trying to pass List from one screen to another. The data is being passed from the async function. I need to open the other Screen based on the data fetched from the database. The answer mentioned here didn't work for me. here is the error.

: [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)    

Code in async function

 Navigator.push(context, MaterialPageRoute(builder: (context) {
    return ParentHome(childList);

ParentHome Screen

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

I am not finding any help regarding this. Please help me figure this out. I am a newbie in Flutter yet. Thanks

Flutter Doctor Output

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

async caller code Async function is being called in onPressed of a RaisedButton.

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

回答1:


You have another Navigator.pop(context); after the switch block. Try removing that one as well or setting a default case if the intention was to navigate back if none of cases match.

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

or

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


来源:https://stackoverflow.com/questions/59801993/flutter-nosuchmethoderror-the-getter-focusscopenode-was-called-on-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!