Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve current className in flutter?

Tags:

I have a class:

class Dog {

Dog() {
  print("Current classname is: ${this.className}");
}

}

It seems this.className is not allowed. Is there something equivalent ?

like image 868
fvisticot Avatar asked Oct 11 '18 16:10

fvisticot


People also ask

How do you get the class name in darts?

String type = MyClass(). runtimeType. toString();

What is of () in flutter?

In the Flutter SDK the . of methods are a kind of service locator function that take the framework BuildContext as an argument and return an internal API related to the named class but created by widgets higher up the widget tree.

How do you name a class in flutter?

Classes, enums, typedefs, and extensions name should be in UpperCamelCase.

How do I know my dart data type?

To check the type of a variable in Flutter and Dart, you can use the runtimeType property.


2 Answers

this.runtimeType 

or just

runtimeType 

returns the name of the current class.

like image 152
Günter Zöchbauer Avatar answered Sep 19 '22 01:09

Günter Zöchbauer


If you want to initialize a variable at the beginning of the class and not in the build method (for instance), the way to go is simply doing as follows:

String className = (YourClass).toString();
like image 40
Iván Yoed Avatar answered Sep 18 '22 01:09

Iván Yoed