usef
usef

Reputation: 11

How to fix "the method 'cancel' called on null" while working with http requests flutter

I'm trying to athenticate using APIs from a flutter app but i get these errors everytime i click Login Button

      final resp = await http.post("http://192.168.73.5/myserv/login.php", body: {
        "login": "login",
        "apid": "re0b53fd92d4b1593db1880az322d66ea9d4",
        "email": _email,
        "pass": _password,
      });
      var __data =json.decode(resp.body);

      if (__data.length == 0) {
      final snackbar = SnackBar(
        content: Text('Server error'),
      );
      scaffoldKey.currentState.showSnackBar(snackbar);
    } else if (__data[0]['resp'] == 'error') {
      final snackbar = SnackBar(
        content: Text('Password or email is incorrect!'),
      );
      scaffoldKey.currentState.showSnackBar(snackbar);
    } else if (__data[0]['resp'] == 'sucess') {
      final snackbar = SnackBar(
        content: Text('You are logged in'),
      );
      scaffoldKey.currentState.showSnackBar(snackbar);
      Navigator.of(context)
          .pushReplacement(MaterialPageRoute(builder: (context) => HomeApp()));
    }

    }

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (29517): The following NoSuchMethodError was thrown while finalizing the widget tree: I/flutter (29517): The method 'cancel' was called on null. I/flutter (29517): Receiver: null I/flutter (29517): Tried calling: cancel() I/flutter (29517): When the exception was thrown, this was the stack:

Upvotes: 1

Views: 931

Answers (1)

Alberto
Alberto

Reputation: 21

My suggestion would be to take a look to your dispose method. There you might notice a statement calling a cancel method on something that was never initiated or used, only declared. In my case I got this error because at the disposed method I was trying to cancel a subscription to a Firebase service that I had not used. I never attached a listener to it, therefore when trying to cancel it, Flutter complained saying "the method cancel was called on null". I deleted the unnecessary line at dispose method and the error resolved. Hope the explanation helps somebody.

Upvotes: 2

Related Questions