{

Flutter runtimeType and is operator| Flutter runtime type of an object


This tutorial shows multiple ways how to check a runtime of an object.

Runtime type and is operator  Dart and flutter example

In Dart programming, there are two ways we can know the type of an object or a variable.

  • runtimeType
  • is operator

This checks dynamic and static types and helps developer to find the following things

  • Check a runtime type of an object
  • Object is assignable to another type using is operator.
  • Check type of an variable declared in Dart or Flutter.

Dart runtimeType

runtimeType is a property instance member available in a superclass( Object). Each class inherits Object, so it provides this property by default.

This helps developers to get debugging information about the type of the variable or an object.

Here is the syntax.

Type get runtimeType

This returns a Static or dynamic type and the runtimeType instance member is not overridden in classes.

Example:

class Employee {}

main() {
  var emp = new Employee();
  var str = "hello";
  var number = 12;
  var float = 12.21;

  List<String> numbers = ["one", "two"];
  print(emp.runtimeType); //Employee
  print(str.runtimeType); //String
  print(number.runtimeType); //int
  print(float.runtimeType); //double
  print(numbers.runtimeType); //JSArray<String>
}

Dart is the operator

is operator used to check if an object is an instance of the type.

Here is an is operator example

object is type

Let’s create an Animal parent class and two child classes.

class Animal {
  eat() {
    print("Animal Eat");
  }
}

class Lion extends Animal {
  eat() {
    print("Lion Eat");
  }
}

class Dog extends Animal {
  eat() {
    print("Dog Eat");
  }
}

main() {
  Animal animal = new Animal();
  Dog dog = new Dog();

  print(animal.runtimeType); // Animal
  print(dog is Animal); // true
  print(animal is Animal); // true
  print(animal is Dog); // false
}

animal is Animal: returns true as the animal object is of Animal class type. dog is Animal returns true as the dog is an instance of subtype, so it returns true. animal is Dog: return false animal is not the type of a class subclass of type.

is always return true if the object is of the same type or subclass type.

Conclusion

As a result, runtimeType is used to check an object type and returns always a string. is the operator used to check an instance of a particular type? Both things help developers to debug the type of an object and identify the type of assignable types to subtype.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example