Dart: checking if an object implements interface

1,179

In Dart a class can only inherit from one superclass (for Place it is Entity) and implement from multiple (i.e. grab functions from those classes).

As far as I could understand from your description, you are doing something like this (you can run it here).

class Person extends LivingCreature implements Mammal{
  final _name;
  void _toDrinkMilk(){
    print("I drink milk as a person!");
  }
  Person(this._name);
}

class Mammal {
  void _toDrinkMilk(){
    print("I drink milk!");
  }
}

class LivingCreature {

  void _toLive(){
    print("I live!");
  }
}

void main() {
  final bob = Person('Bob');
  final mammal = Mammal();
  print(mammal is LivingCreature);
  print(bob.runtimeType);
  bob._toDrinkMilk();
  bob._toLive();
  print('Bob is mammal: ${bob is Mammal}');
  print('Mammal is Person: ${mammal is Person}');
}

And this code yields following output:

true
Person
I drink milk as a person!
I live!
Bob is mammal: true
Mammal is Person: false

So, now let us talk about what just happens in your case, your like.likable should be a Likable abstract class (I assume). Place in the meantime implements Likable, so it should implement all methods of Likable.

Now, you are trying to ask Dart is the type of your Likable (which does not extend or implement anything) is Place, which is false as given in the log.

If it has been class Likable extends Place {... then in the log you would have like.likable is Place be equal to true

For more on extends, implements, with you can refer to the Dart docs

P.S. I did not make Mammal abstract to instantiate it quickly in print()

Hope it helps!

Share:
1,179
Anuar Otynshin
Author by

Anuar Otynshin

Updated on December 21, 2022

Comments

  • Anuar Otynshin
    Anuar Otynshin over 1 year

    I have the following classes and interfaces:

    class Place extends Entity
        with ChangeNotifier, FieldGetters
        implements Scannable, Likable {
    // ...some stuff
    }
    
    
    abstract class Likable {
    // ... some stuff
    }
    
    

    Entity is just a base class for other classes.

    So, the problem is that I want to check whether the object that implements the Likable interface is of class Place or not. To do so, I use the following code:

    logger.i('likable is ${like.likable.runtimeType}');
    logger.i('likable is Place: ${like.likable is Place}');
    

    The first line works as expected and shows the following output:

    I/flutter ( 1635): [38;5;12m┌───────────────────────────────────────────────────────────────────────────────[0m
    I/flutter ( 1635): [38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄[0m
    I/flutter ( 1635): [38;5;12m│ 💡 likable is Place[0m
    I/flutter ( 1635): [38;5;12m└───────────────────────────────────────────────────────────────────────────────[0m
    

    However, the second line of code acts weird and shows the following:

    I/flutter ( 1635): [38;5;12m┌───────────────────────────────────────────────────────────────────────────────[0m
    I/flutter ( 1635): [38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄[0m
    I/flutter ( 1635): [38;5;12m│ 💡 likable is Place: false[0m
    I/flutter ( 1635): [38;5;12m└───────────────────────────────────────────────────────────────────────────────[0m
    

    The official documentation suggests that the interface inheritance can be checked via is keyword. I am confused. How can I check the interface inheritance in this case?

    Dart: v2.8.1

    Flutter: v1.17.0