Flutter Web Error Handling

139 views
Skip to first unread message

Kevin Roberson

unread,
Feb 6, 2021, 12:26:10 AM2/6/21
to Flutter Development (flutter-dev)
Hello,

I'm working on a user registration page for the web and could use some help with error handling.  I have a registration page that presents a form and then calls the addUser() function to send an http post to my api server, which in turn sends the sql insert to the database.  It works fine when there are no errors and I present a 'success' page.  What I'm trying to do is present an alert window when an error occurs that notifies the user that the registration was not successful and support has been notified.  At that point, I send an email containing the error and the user's email to my support inbox so I can notify them when the problem is resolved.  The trouble I am having is I can't seem to isolate the success code from the error code.  When I test the code below by ending the api server or database server, I always hit the if statement that tests the response code and it is of course null because the post failed before it could return with a valid response.  (Error message:  
TypeError: Cannot read property 'statusCode' of null)

I was expecting the if block to be skipped when an error occured with the post and the catch block would be executed, but evidently it doesn't quit work that way.  Is there another way to have the if block not execute if there is an error?  I must be missing something obvious.

Thanks for any help or suggestions.

Kevin


Future addUser() async {
  http.Response response;
...

  try {
      response = await http.post(
        Constants.API_URL + '/addUser',
        headers: <String, String>{'Content-Type': 'application/json'},
        body: json.encode(regForm),
      );

      if (response.statusCode == 200) {
        Navigator.of(context).pushNamed(Success.route);
      }
    } catch (e) {
      String subject = Messages.ERROR_USER_REGISTRATION_FAILED;
      String error =
          'Error registering user $user.  Error: e.toString()';  

      errorMsg(context, user, subject, error);
    }
  }
}

Suzuki Tomohiro

unread,
Feb 6, 2021, 1:02:24 AM2/6/21
to Kevin Roberson, Flutter Development (flutter-dev)
because the post failed before it could return with a valid response.  (Error message:  
TypeError: Cannot read property 'statusCode' of null)

How did it fail? No connection to server, or server did return 400 or 500 status code?


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/519f8b6a-a0aa-4859-bc7d-7b693455c616n%40googlegroups.com.

Kevin Roberson

unread,
Feb 6, 2021, 8:24:33 AM2/6/21
to Flutter Development (flutter-dev)
No response code.  It is null.  Just the error on the statusCode property.  Also, I started this in VS Code with 'Run without debugging', but yet it still started the debugger.  Here is the console log:

Launching lib\main.dart on Chrome in debug mode...
 lib\main.dart
Debug service listening on ws://127.0.0.1:51079/BNHIyB0D6rw=/ws
Running with unsound null safety
Connecting to VM Service at ws://127.0.0.1:51079/BNHIyB0D6rw=/ws
TypeError: Cannot read property 'statusCode' of null
    at registerForm._RegisterFormState.new.addUser (http://localhost:51013/packages/monnie/screens/registerForm.dart.lib.js:3055:119)
    at addUser.throw (<anonymous>)
    at _RootZone.runBinary (http://localhost:51013/dart_sdk.js:37866:58)
    at _FutureListener.thenAwait.handleError (http://localhost:51013/dart_sdk.js:32834:48)
    at Function._propagateToListeners (http://localhost:51013/dart_sdk.js:33412:17)
    at _Future.new.[_completeError] (http://localhost:51013/dart_sdk.js:33258:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:51013/dart_sdk.js:33297:31)
    at Object._microtaskLoop (http://localhost:51013/dart_sdk.js:38122:13)
    at _startMicrotaskLoop (http://localhost:51013/dart_sdk.js:38128:13)



Reply all
Reply to author
Forward
0 new messages