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")
Example:
In this example, we'll be creating a Custom Library and then show its metadata using Symbols.
Here, we have first created a Dart File Custom.dart containing a library custom_lib, which consists of a class called CustomClass having methods customFunction1 and customFunction2.
Dart
library custom_lib;
class CustomClass {
void customFunction1() {
print("Custom Function 1");
}
void customFunction2() {
print("Custom Function 2");
}
}
Next, we'll create the main.dart file, which will import the Custom. dart file along with a couple of other libraries such as dart:core and dart:mirror
- First, we'll create 2 symbols. One for the custom library and the other for the custom class.
- Next, we've to initialize the mirror system, and create a mirror reference to our custom_lib library, and then using that, to our CustomClass class.
- Using classMirror, we can now extract some data i.e. a list of methods present in our class. That includes -
- User created methods such as customFunction1
- In-build methods such as toString
Dart
import 'dart:core';
import 'dart:mirrors';
import 'Custom.dart';
void main() {
// Create symbols for library and class
Symbol libraryName = Symbol('custom_lib');
Symbol className = Symbol('CustomClass');
// Init mirror system
MirrorSystem mirrorSystem = currentMirrorSystem();
LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);
ClassMirror classMirror = libMirror.declarations[className];
// Get and print methods
final methods = classMirror.instanceMembers;
print('Total number of methods = ${methods.length}');
methods.forEach((symbol, method) {
print(symbol);
});
}
Output:
Total number of methods = 7
Symbol("==")
Symbol("hashCode")
Symbol("toString")
Symbol("noSuchMethod")
Symbol("runtimeType")
Symbol("customFunction1")
Symbol("customFunction2")
Similar Reads
HTML Symbols HTML Symbols are special characters used in HTML to display characters that aren't on the keyboard or might cause issues with HTML code. Like if you used < (less than) or > (greater than) symbols in your HTML document then the browser will treat them differently.To define HTML symbols, you can
6 min read
Algebra Symbols Algebra Symbols are specific characters that are used to represent particular operations in Algebra. The branch of Algebra deals with the relation between variables and constants. There are different branches of Algebra such as linear algebra, vector algebra, and Boolean algebra for which we have di
12 min read
Dart - Sets Sets in Dart is a special case in List, where all the inputs are unique i.e. it doesn't contain any repeated input. It can also be interpreted as an unordered array with unique inputs. The set comes into play when we want to store unique values in a single variable without considering the order of t
6 min read
Dart - Keywords Dart Keywords are the reserved words in Dart programming Language which has some special meaning to the compiler. These keywords are case-sensitive and cannot be used to name variables, classes, and functions. There are in total 61 keywords in Dart programming language: Â Keywords Tableabstract
15+ min read
Calculus Symbols Calculus symbols are symbols used in calculus in mathematics. There are different calculus symbols including limits symbols, derivatives, integrals, definite integrals, vector calculus, etc. In this article, we will explore all the calculus symbols that are useful in representing different types of
4 min read