Error Handling Flutter

There are currently no recipes or documentation explaining how to handle errors in Flutter.

Exactly what methods of the User Service API can be used to handle errors?

For the REST API I found the getCode() method. Something like this would be pretty useful for Flutter.
https://backendless.com/docs/android/shared_error_handling.html

I attempted:

      await Backendless.userService.login(email, password).then((user) =>
          Navigator.push(
            context,
            CupertinoPageRoute(
              builder: (context) => Home(),
            ),
          )
      );
    }
    catch (user) {

BackendlessFault fault= Backendless.userService.login(email, password).catchError(error);
}

Hi @Samuel_Franco!

To handle errors in Flutter, you should use method catchError like this:

void logIn(email, password) {
  Backendless.userService.login(email, password)
    .then((loggedInUser) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => Home(),
        ),
      );
    })
    .catchError((onError) {
      showBasicAlert(context, "Wrong username or password", "");
    });
}

Best Regards,
Maksym

Hello, Maksym. Thanks for your reply!

I didnt male a good description before. What I actually need is a way to store the error message which contains an error code and description. By substringing the code I would proceed to display the corresponding alert.

If its Erorr 3033: User already exists for example, I would use 3033 to identify this and then proceed to modify my widgets so the user is able to know it was the email that he must change.

If its error 3003: No users found with this id, then I would print “There are no users registered with this email” and so on.

I got it, @Samuel_Franco

In this case you need to check, if the error is PlattformException and extract the code and message from it:

 .catchError((onError) {
  if (onError is PlatformException) {
    print("Error code: " + onError.code);
    print("Error message: " + onError.message);
  }
});

You will get the output like this:

Error code: 3003
Error message: Invalid login or password

Best Regards,
Maksym

Thanks, Maksym!

Do you know how to identify whether what is wrong is the user or the password?

Perhaps its clearer this way:

If I get an email by user input, how can I search for a user in the Users table who has this email registered? The table property “email” is marked as the identifier.


If there is no user with this email I would alert: “User doesnt exist”

If there is, then Id proceed to attempt login and inform “Wrong Password”.

This is very important from the UX perspective.

This is what I have so far:

void _loginUser2() async {

    String myEmail=email;

    DataQueryBuilder query = DataQueryBuilder();

    query.whereClause = "email LIKE 'myEmail%'";

    var users = userTable.find(query);

    print("Datos de Tabla Usuario");

    userTable.find(query).then((users) {

      for ( Map user in users) {
        print("""
        Email - ${user['email']}
        Phone Number - ${user['phoneNumber']}
        ============================""");
      }
    });

    //if( users.data.count > 0 )

  }

Hello,

It is not a good idea to provide information to the user on whether it is email or the password is incorrect. This provides a clue to a hacker to break into the system by identifying a valid email and then trying different passwords until they break in.

As for checking if a user with a specific email exists, the where clause should look like this:

email = 'value of the email address to check'

Regards,
Mark

Hi, I am trying to do a simple register and handle exceptions like 3033 (email address already used) , however this code just throws the raw exception and crashes the app. It is not being caught in the catch code.

What am I missing?

Backendless.userService.register(user).then((response) {
//handle success
})

.catchError((error) {
if (error is PlatformException) {
if (error.code as int == 3087) {
//handle error
});

Thanks

Hello, @Carlton_Branch,

Please create a separate topic for this issue, this one is already closed.

Regards,
Olha