Zahal A_Omer
Zahal A_Omer

Reputation: 163

how to find all properties and methods of specific object in dart?

in python, there is a built-in function called dir which displays all properties and methods of the specified object so is there a function like that in Dart if it's what is called? and also in python, there is another function called type which displays the type of the argument you give so is there a function like that in Dart also?

Upvotes: 2

Views: 453

Answers (1)

julemand101
julemand101

Reputation: 31299

Dart does not have this kind of feature in runtime unless you are running your code with the Dart VM, in which case you can use dart:mirrors: https://api.dart.dev/stable/2.13.4/dart-mirrors/dart-mirrors-library.html

One reason for this restriction is that Dart are a compiled language, so when compiled to native or JavaScript, the compiler identifies what code are used and only takes what is needed for your program to be executed. It also makes optimizations based on how the code are being used. Both kind of optimizations is rather difficult if you are allowing every method to be accessed.

The Dart VM can do this since it does has access to all the code when running the program.

There are some ongoing discussion about adding meta programming to Dart which might be the solution for most cases where dart:mirrors are used today:

https://github.com/dart-lang/language/issues/1518

https://github.com/dart-lang/language/issues/1565

An alternative solution is use some kind of builder which makes static analysis of your code and compiles some classes based on that which you are then includes in your project. E.g. a class which contains the name and type of all members of the classes you need that information from.

Upvotes: 3

Related Questions