Symbol Creation
In Dart, Symbols can be made in one of two ways:
Using #
When a Hash (#) symbol is added to an identifier, it becomes a Symbol.
Code:
print(#my_symbol);
Output:
Symbol("my_symbol")
Using Class constructor
Otherwise, a symbol can be explicitly constructed with the Class constructor.
Code:
Symbol my_Symbol = Symbol('my_symbol');
print(my_Symbol);
Output:
Symbol("my_symbol")
Let us see an example where we will be using Dart Symbol for its better understanding and approach.
Example
In this example, we'll create a Custom Library file and then use Symbols to display its metadata.
So, below we have first created a Dart File named myCustomFile.dart containing a library mycustom_lib, which consists of a class called myCustomFileClass having methods m1 and m2.
myCustomFile.dart
library mycustom_lib;
class myCustomFileClass {
void m1() {
print("my Custom file's- Function 1 m1");
}
void m2() {
print("my Custom file's- Function 2 m2");
}
}
The next step is to create the myMainFile.dart file, which will import the myCustomFile.dart file as well as a few additional libraries like dart:core and dart:mirror.
myMainFile.dart
import 'dart:core';
import 'dart:mirrors';
import 'myCustomFile.dart';
void main() {
// Step1: Creating the symbols for our library and class
Symbol mylibraryName = Symbol('mycustom_lib');
Symbol myClassName = Symbol('myCustomFileClass ');
// Step2: Using the mirror system
MirrorSystem myMirrorSystem = currentMirrorSystem();
LibraryMirror my_libMirror = myMirrorSystem.findLibrary(mylibraryName);
ClassMirror my_classMirror = my_libMirror.declarations[myClassName];
// Step3: counting and printing the methods
final my_methods = my_classMirror.instanceMembers;
print('Total methods = ${my_methods.length}');
my_methods.forEach((symbol, method) {
print(symbol);
});
}
Output:
Total methods = 7
Symbol("==")
Symbol("hashCode")
Symbol("toString")
Symbol("noSuchMethod")
Symbol("runtimeType")
Symbol("m1")
Symbol("m2")
Conversion
Convert Symbol to String: The built-in class MirrorClass, offered by the dart:mirror package, can be used to transform the Dart symbol to a string. Let's take a look at the following example.
Code:
import 'dart:mirrors';
void main(){
Symbol my_lib = new Symbol("my_custom_lib");
String name_of_my_lib = MirrorSystem.getName(my_lib);
print(my_lib);
print(name_of_my_lib);
}
Output:
Symbol("my_custom_lib")
my_custom_lib
Frequently Asked Questions
What does => mean in Dart, and how do you construct a Dart symbol?
Dart programming, you may have noticed the => sign. This arrow syntax is used to define a function that executes and returns the value of the expression to its right. And, A hash (#) followed by the identifier name can be used to generate a symbol for an identifier.
In Dart, what is the difference between final and Const?
In Dart, the Const keyword works in the same way as the final keyword. The only distinction between final and const is that const makes the variable constant only at compilation time.
Does Dart have/support multiple interfaces?
Dart has the potential to implement multiple interfaces. The keyword implements enable a class to adhere to several interfaces, extending the polymorphic range of an object. The keyword implement is followed by the name of an existing named class, whose public fields are subsequently used to provide implementation requirements for the present class.
Conclusion
In this article, we learned about Dart Symbol, their creation, and their conversion with a working example.
You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
We hope this article has helped you enhance your knowledge of Dart programming and Input-Output in it. If you want to learn more, check out our Programming Fundamentals and Competitive Programming articles. Do upvote this article to help other ninjas grow.