0

I have a class A with some properties:

abstract class A {
  double doubleA;
  String stringA;
  ...
  A({this.doubleA = 0, this.stringA = ""});
}

and a class B with some properties, that extends class A:

class B extends A {

  int intB;
  String stringB;

  B({
    this.intB = 0, 
    this.stringB = "",
    double doubleA = 0, 
    String stringA = "",
  }) : super(doubleA: doubleA, stringA: stringA);
}

In my code I want to now check if an instance of A has a value that is of type of subclass B:

A a; // Value can be of different subtypes of A including B
if(a is B) {
  // here dart should give me access to the properties of a like:
  print(a.stringA);
  // but it should also be possible to access the type B properties 
  // since the value of a can also be of subclass type B:
  print(a.stringB);
}

This sounds wrong at first but I know that it can work because of examples in flutter.

Example Listener:

Listener(
  onPointerSignal: (event) {
    // event is of type PointerSignalEvent which has no property 'scrollDelta'.
    // So print(event.scrollData); does not work here.
    if (event is PointerScrollEvent) {
      // if you check if event is of subtype PointerScrollEvent the property 'scrollDelta'
      // that is included in the class PointerScrollEvent becomes available.
      print(event.scrollDelta); // works without any problem.
    }
  },
}

However I have not been able to replicate this with my classes A and B and I don't know why it doesn't work. I have also looked into the implementations of these flutter classes and copied the class structure but I can still only access the properties of A after the check if(a is B) which doesn't correspond to the behavior observed with the flutter classes.

What am I doing wrong? Am I am missing something?

Thanks for reading :D <3

MindStudio
  • 706
  • 1
  • 4
  • 13
  • We can't tell what you're doing wrong if you don't provide a specific, reproducible example of where it doesn't work for you. Note that `if (a is B) { print(a.stringB); }` will work [only if `a` is a *local variable*](https://stackoverflow.com/q/56764592/). – jamesdlin Aug 12 '21 at 17:27

1 Answers1

0

If you declare a variable 'a' as being of type 'A', you will not be able to access the properties of classes inheriting from A (like B). Let's say A is Animal and B is Baboon. Baboon inherits properties from Animal, so all variables instantiated with the Baboon type with have access to properties from both classes. But variables instantiated with the Animal type will only have access to the Animal properties. Here are some examples: https://medium.com/jay-tillu/inheritance-in-dart-bd0895883265

  • It works for now if i just replace `A a;` with `var a`. So thank you very much already. But what if I have a `List` that has animals of type Baboon inside of them... can I make that work or do I need a `List` of undefined type for that? Because then I cannot even access the properties that all animals should have, and that would cause problems. Edit: I basically want the "default" properties of A an all Animals even when I don't check the type. – MindStudio Aug 12 '21 at 14:34
  • By using the var keyword, you let Dart define the type itself, so it can probably determine from your code that the variable a has type Baboon. You can have a List containing variables of type Animal and Baboon, and as long as you tell which are which, you can then do type checks and access the Baboon properties for the items of that type. – Louis Deveseleer Aug 12 '21 at 14:37
  • That's the problem. I created a list of animals with a baboon inside. If i now check the type with `if(animals[0] is Baboon)` i can still only access the animal properties but not the baboon properties. `print(animals[0].baboonProperty)`- "the getter 'baboonProperty' is not defined for the type Animal..." – MindStudio Aug 12 '21 at 14:44
  • 1
    Yeah I think you're right, you would either need to have a list of undefined type, or a list of Baboon, but if it's a list of Animal I don't know how to differentiate. – Louis Deveseleer Aug 12 '21 at 16:30
  • 2
    "If you declare a variable 'a' as being of type 'A', you will not be able to access the properties of classes inheriting from A (like B)". `A a = B();` allows you to use `B`'s methods *if* you explicitly cast with `as` or if type promotion occurs with `a is B`. The reason why `animals[0] is Baboon` does not promote `animals[0]` to `Baboon` is because [automatic type promotion occurs *only for local variables*](https://stackoverflow.com/q/56764592/). – jamesdlin Aug 12 '21 at 17:24
  • thx @jamesdlin and Louis that was the information i was not able to find :D <3 – MindStudio Aug 12 '21 at 20:27