You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently we don't have a possibility to handle Exceptions that happens inside a Widget somewhere down the widget tree. I would propose a Widget that allows that. Something like
typedefExceptionHandler=voidFunction( ex)
typedefAlternativeBuilder=voidFunction(BuildingContext context, ex)
ExceptionHandledBuilder(
List<Object> exceptionsToCatch, // Exceptions that should behandled (It's really a pity that Dart doesen't have a common Exception base class)HandlerFunction: onException, // get's called to allow logging or what elseAlternativeBuilder: alternativeBuilder, // Builder that get's invoked while executing the normal builder methodAsyncWidgetBuilder<T>: builder); // the default builder that tryies to bild the child widget tree.
I don't have a lot of thoughts from the language perspective (as opposed to the API perspective), other than to wonder whether support for implicitly lazy parameters would clean this up a bit? cc @munificent
@Hixie if I call a function to assign a value inside the widget tree that could throw an exception I have no chance to handle this. Sure I could do such calls at the beginning of the build method with a try-catch and set some bool flags to display different contents but I think there should be a better way to achieve that
Ah, so the issue is that in the middle of building a tree of widgets, an exception might occur. You want to be able to catch it and provide an alternate subtree instead of the failed one?
Yeah, that is a completely feasible solution. My suggestion from the beginning was if something like that shouldn't be part of the framework. @eseidelGoogle
I would recommend making your build methods not ever throw. Don't read data from a database within your build methods, for example; read the data in your model objects, asynchronously, and have the build methods read from your model in a way guaranteed not to throw.
But if that's not a thing you want to do, consider making a variant of Builder that catches exceptions, something like (untested):
@Hixie that looks like an interesting idea. I agree with you that normally this should not happen. Anyway I still think such a widget should be part of the framework in case you need it e.g. if a Widget from a package throws exception under certain conditions.
We don't really want to encourage people to consider it to be ok to throw in a builder. We technically already wrap most builders in a try/catch, that's how ErrorWidget.builder gets called.
@rrousselGit I think to revive this discussion we're going to need to engage in something a bit more long-form. Can you provide examples of what you're trying to achieve? (e.g. I want to have a click-handler which might throw and when it does I want to do X.)
In any case, this bug is long and it might be time to start a new one, clearly stating the problem and desired outcome and let folks hash out what the right solutions then should be?
I'm currently faced with the situation that it can happen that a file a FileImage tries to load does not exists. Currently there is only a loadingBuilder but not an ErrorBuilder. This just as an example.
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 flutter doctor -v and a minimal reproduction of the issue.
Activity
eseidelGoogle commentedon Jul 30, 2018
@Hixie and @leafpetersen are the right folks for thoughts probably.
leafpetersen commentedon Jul 30, 2018
I don't have a lot of thoughts from the language perspective (as opposed to the API perspective), other than to wonder whether support for implicitly lazy parameters would clean this up a bit? cc @munificent
Hixie commentedon Jul 30, 2018
@escamoteur What's the use case here?
zoechi commentedon Jul 31, 2018
I guess the first bullet of #19996
escamoteur commentedon Jul 31, 2018
@Hixie if I call a function to assign a value inside the widget tree that could throw an exception I have no chance to handle this. Sure I could do such calls at the beginning of the build method with a try-catch and set some bool flags to display different contents but I think there should be a better way to achieve that
munificent commentedon Aug 2, 2018
Ah, so the issue is that in the middle of building a tree of widgets, an exception might occur. You want to be able to catch it and provide an alternate subtree instead of the failed one?
Something like:
And you want to be able to handle that exception and swap out the widget without having to do:
Do I have that right? You can always solve this at the application level by writing a utility function:
If Dart were an expression-oriented language, I could imagine that it would allow something like:
I'm definitely not proposing that, but that is a way it could be solved in the language.
As far as your original question, yes, it's possible. Here's one way:
Then you'd use it like:
It would be easy to extend
handleException()to let you control which exceptions it catches.zoechi commentedon Sep 3, 2018
@escamoteur what do you think about @munificent's suggestion?
escamoteur commentedon Sep 5, 2018
Yeah, that is a completely feasible solution. My suggestion from the beginning was if something like that shouldn't be part of the framework. @eseidelGoogle
Hixie commentedon Sep 12, 2018
I would recommend making your build methods not ever throw. Don't read data from a database within your build methods, for example; read the data in your model objects, asynchronously, and have the build methods read from your model in a way guaranteed not to throw.
But if that's not a thing you want to do, consider making a variant of Builder that catches exceptions, something like (untested):
If you want custom handling for the error, you can call a different builder instead of ErrorWidget.
Also you may wish to look at ErrorWidget.builder, which is a more global way to catch errors.
escamoteur commentedon Sep 18, 2018
@Hixie that looks like an interesting idea. I agree with you that normally this should not happen. Anyway I still think such a widget should be part of the framework in case you need it e.g. if a Widget from a package throws exception under certain conditions.
Hixie commentedon Oct 6, 2018
We don't really want to encourage people to consider it to be ok to throw in a builder. We technically already wrap most builders in a try/catch, that's how
ErrorWidget.buildergets called.goderbauer commentedon Jan 3, 2019
Closing based on the comment above from @Hixie.
rrousselGit commentedon Jul 2, 2019
#19993 (comment)
This is not a solution. Error boundaries are not just about build methods.
They are for any kind of exceptions that comes from the descendants. This also includes:
The exception can also happens from a widget very deep down in the widget tree.
The catcher is not necesseraly the widget that threw.
eseidelGoogle commentedon Jul 9, 2019
@rrousselGit I think to revive this discussion we're going to need to engage in something a bit more long-form. Can you provide examples of what you're trying to achieve? (e.g. I want to have a click-handler which might throw and when it does I want to do X.)
There is discussion of error reporting in https://flutter.dev/docs/cookbook/maintenance/error-reporting. As Ian notes, there is already an ErrorWidget. But it's not clear from your comment what your asking for.
In any case, this bug is long and it might be time to start a new one, clearly stating the problem and desired outcome and let folks hash out what the right solutions then should be?
escamoteur commentedon Mar 12, 2020
I'm currently faced with the situation that it can happen that a file a FileImage tries to load does not exists. Currently there is only a loadingBuilder but not an ErrorBuilder. This just as an example.
escamoteur commentedon Mar 12, 2020
The handleException og @munificent solved it for now. I still think we should have something like this in the framework
escamoteur commentedon Mar 12, 2020
I have to correct me. The exception that is thrown by the imagestream never reaches the Widgettree. it is swallowed somewhere.
lock commentedon Apr 4, 2020
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
flutter doctor -vand a minimal reproduction of the issue.