Table of contents
1.
Introduction
2.
Symbol In Dart
3.
Symbol Creation
3.1.
Using  #
3.2.
Using Class constructor
3.3.
Example
4.
Conversion
5.
Frequently Asked Questions
5.1.
What does => mean in Dart, and how do you construct a Dart symbol?
5.2.
In Dart, what is the difference between final and Const?
5.3.
Does Dart have/support multiple interfaces?
6.
Conclusion 
Last Updated: Mar 27, 2024

Dart Symbols

Author Akshit Pant
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Symbols are an object representation of either an identifier or an operator in Dart. Dart's symbols are opaque and dynamic string names that can't be modified and stay the same throughout the compilation process.

In this article, we will be learning about Dart Symbols, their use-cases & conversion with an example.

Before learning how we do so, learning the basic Introduction of Dart is recommended.

So, let us start reading about Symbol in Dart.

Symbol In Dart

A symbol object in Dart represents a declared operator or identifier in a Dart program. Because symbols are compile-time constants, they cannot be modified. Because modification replaces identifier names but not identifier symbols, they are extremely useful for APIs that refer to identifiers by name.

Symbols can be used to obtain various information about the type (library, class, etc.) For example, a list of instance methods, constructors, and so on.

Note: Symbols are rarely used nowadays, and libraries that support them, such as the MirrorSystem, have been deprecated.

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.

Live masterclass