New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch a Thrown Exception from Another Function #22734
Comments
It's easier to use class LoginScreenPresenter {
LoginScreenContract _view;
RestDatasource api = new RestDatasource();
LoginScreenPresenter(this._view);
doLogin(String username, String password) {
try {
var user = await api.login(username, password);
_view.onLoginSuccess(user);
} on Exception catch(error) {
_view.onLoginError(error.toString());
}
}
} Please consider asking support questions in one of the other channels listed at http://flutter.io/support . |
The *edited |
I just added 'async' in the function declaration, and it works! Thanks! doLogin(String username, String password) async {
try {
var user = await api.login(username, password);
_view.onLoginSuccess(user);
} on Exception catch(error) {
_view.onLoginError(error.toString());
}
} |
Does the .catchError method simply not work? |
@Mikkal24 sure it does, but it's a bit tricky to get it right for all cases. |
I think I got it to work but, my debugger is still dealing with the exception as if it was un-handled. Separate issue I think. |
@Mikkal24 please report in https://github.com/dart-lang/sdk/issues |
@zoechi -- The issue I have with your suggestion is that there are many cases where await can't be used in Flutter... For example, in form validation (validators) .. where you have to be "sneaky" and use things like .then() within a sync function for it to be happy and not have the linter complain right and left (using the Google linting rules) .. Since I am getting the same error -- I am wondering if there is a more concrete explanation of what the error means (it's certainly opaque at best) ... |
@sjmcdowall if you mean You can call async functions from everywhere. There are some places where Flutter does not wait for async results because it doesn't make sense because it needs to provide a result for the next screen render ( I would need more details to give concrete advice, |
@zoechi -- Hey Günter! I guess what I was saying -- for a concrete example-- is I have a validator function in a form field. If I try to use an async function Future<String> _myValidator(String value) async {} Then the linter complains (or compiler?) that the return type of I would think this is a good idea as part of the "Cookbook" .. how to integrate Anyway, the original error Cheers!! |
@sjmcdowall
I'd consider that a good thing because it tells you that what you are attempting works agains the framework.
I think this one is covered pretty well.
For async validator you can thumbs-up and follow #9688 If there are others check if there exists an issue already or create one. I can't really make sense of your last paragraph. |
I had the same problem of the original poster, but in my case I missed an "await" calling the function "A" from the function "B" (thank you for the hint given by the answer) so the catch in the function "B" (that I wanted to show) was evaluated before the catch of the function "A" (because in that moment there were no error yet raised by the function "A", therefore the catch in the function "B" was not invoked). Adding the missing "await" (it was my failure) calling the "A" function, the catch of the function "B" catched the error raised by the function "A" as expected and the catch of the function "B" showed the error thrown by function "A". |
.catchError((error) { |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
Hi,
I am new to Flutter, I have a problem when I am trying to catch a thrown exception from another function. Here is the source code that I got from here.
The function A that throws an Exception
The function B that catches an exception then show it to the user
However, there is a runtime error in function A:
I'm stuck in this error and already looked for the solution but still no result.
The text was updated successfully, but these errors were encountered: