Trusted answers to developer questions

What is a Dart symbol?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

In Dart, a symbol object represents an operator or identifier declared in a Dart program. Symbols cannot be changed as they are compile-time constants. This makes them invaluable for APIs that refer to identifiers by name because minification replaces identifier names, but not identifier symbols.

Symbols are rarely used these days and libraries, like the MirrorSystem that supports them, have been deprecated.

Minification reduces file size to send files over a network
Minification reduces file size to send files over a network

Usage

Prefixing any identifier with # makes it into a symbol literal:

#foo
#bar
// To get the symbol for an identifier, use a symbol literal, which is just # followed by the identifier

The syntax to declare a symbol is:

import 'dart:convert';
void main() {
Symbol object = new Symbol('name');
// name must be a valid public Dart member name, public constructor name, or library name.
print(object);
}

RELATED TAGS

dart
programming
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?