-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
type {superclass} is not a subtype of type {class extending it} #37742
Comments
Dart does not auto-convert types like that. You claimed in the Dart Gitter chat that this would work in Java, but that's not the case either. Type rules in Java and Dart are quite similar. What would've worked in either language is something like this: class ResponseWithError {
final http.Response response;
// more fields here?
ResponseWithError(this.response);
} And then, where you make the http call: ResponseWithError response = ResponseWithError(await http.post( .... )); |
I must've been mistake then. I that is a way I considered doing it, but typing out ResponseWithError.response.statusCode felt really clunky which is why I was trying to avoid that |
If you really must, you can implement the delegate pattern so that This can also be implemented with codegen, as was done in zengen, but that hasn't been upgraded to Dart 2.0 yet (it's a lot of work! I am trying to help). I couldn't find a package that currently supports that, unfortunately... Maybe you can write one :) |
Dart's type system is nominal and you can't cast a instance of a supertype to a subtype. Sometimes you might statically have a supertype which is at runtime a subtype, and then the cast would work. As @renatoathaydes points out Java works very similarly here. class A {}
class B implements A {} // could be implements or extends
void main() {
var isA = A();
print(isA is B); // False, an A is not a B
var isB = B();
doStuff(isB);
}
var doStuff(A a) {
print(a is B); // May be true, if at runtime the arg was actually a B
} |
I initially thought I was being stupid, but after thoroughly reviewing the documentation with a coworker I am not entirely convinced it isn't a bug.
I am using the Dart http library and attempting to extend the "Response" object. Basically, my class is extending the "Response" object from the library as well as containing an additional ConnectionError object which is self-explanatory.
I am implementing all the constructor parameters of the http Response class. Therefore, since I am extending it, I should be able to do this:
ResponseWithError response = await http.post(url, headers: headers, body: json.encode(alarmSearchRequest.toJson()));
right?Well the problem is, I get this error:
Unhandled Exception: type 'Response' is not a subtype of type 'ResponseWithError'
I do not understand what could possibly be wrong with this code that makes it not want to cast. I have pasted the ResponseWithError class below (with the custom error code removed, bug is still present with this exact code).
The kicker is, if I copy and paste the http Response class and extend BaseRequest it works fine with casting. Please help me understand this behavior, thanks
The text was updated successfully, but these errors were encountered: