-
Notifications
You must be signed in to change notification settings - Fork 212
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
List runtime subtyping error #1581
Comments
Dart generics are always covariant, which means that there are implied runtime checks which can fail, as you observe. There's currently not a good way to prevent this, though we're exploring adding the option of using statically checked variance annotations to control this better. It would have some cost in terms of convenience however. For this concrete example, I would probably recommend constructing the right kind of list to start with, rather than using I suppose we could lint on using cc @lrhn @munificent @natebosch for any other thoughts. |
Not any great alternatives. Any time you up-cast a list, some methods become unsafe or surprising. Here we have up-cast I think my recommendation would be to never use |
Thanks for the suggestions! I didn't think about the collection literals solution.
Is there any open issue about this? I'm interested in the topic. |
Hello!
I've been migrated some code to null safety, in particular code that generates raw SQLite queries. One of these methods combines 2 arguments lists into one for the final query.
The migration I did involves generalizing the list of arguments to
List<Object?>
, but the problem I faced was a runtime exception when concatenating the twoList<Object?>
. The issue was that one of them was a variable of typeList<Object?>
but in reality it was created asList<Object>
. Passing the non-nullable version as an argument to a function works fine but it can produce hard to catch bugs.Example of the issue I had: https://dartpad.dev/2a154028230eeb58bb03856c020d7b8f?null_safety=true
The first print works fine whereas the second print gives a runtime exception that is hard to catch in a big codebase.
I was wondering if there is any option in the analyzer or lint that makes this issue more obvious to catch.
The solution I found is to do a
.cast<Object?>
but I wish there is a better solution.The text was updated successfully, but these errors were encountered: