In Dart, Symbols are basically an object representation of either an identifier or operator. The symbols in dart are opaque and dynamic string names that cannot be changed and remains constant throughout the compile time. It can be used for reflecting the metadata on a type, such as a library or class. They are generally used for creating APIs and establish a relationship between a string that is understandable to humans and a string that can be used by computers.
Symbols can be used to get various information about a type (library, class, etc) such as a list of instance methods, constructors, etc.
Creating Symbols
Symbols can be created in 2 different ways –
1. Adding a Hash (#) symbol to an identifier would convert it into a Symbol
print(#mysymbol);
Output:
Symbol(“mysymbol”)
2. Otherwise, a symbol can be created explicitly using the Class constructor.
Symbol mySymbol = Symbol(‘mysymbol’);
print(mySymbol);
Output:
Symbol(“mysymbol”)