{"id":3582,"date":"2022-05-19T05:58:38","date_gmt":"2022-05-19T05:58:38","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3582"},"modified":"2022-05-19T05:58:38","modified_gmt":"2022-05-19T05:58:38","slug":"dart-symbol","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/19\/dart-symbol\/","title":{"rendered":"Dart Symbol"},"content":{"rendered":"\n<p>Symbol object is used to specify an operator or identifier declared in a Dart programming language. Generally, we do not need to use symbols while Dart programming, but they are helpful for APIs. It usually refers to identifiers by name, because identifier names can vary but not identifier symbols.<\/p>\n\n\n\n<p>Dart symbols are dynamic string name that is used to derive the metadata from a library. It mainly accumulates the connection between human-readable strings that are enhanced to be used by computers.<\/p>\n\n\n\n<p>Symbols have a term which is called Reflection; it is a technique that used to the metadata of at run-time, for example &#8211; the number of methods used in class, a number of constructors in a class, or numbers of arguments in a function.<\/p>\n\n\n\n<p>The dart:mirrors library has all of the reflection related classes. It can be used with the command-line applications as well as web applications.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p>The\u00a0<strong>hash(#)<\/strong>\u00a0symbol, followed by the name is used to define Symbol in\u00a0Dart. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Symbol obj = new Symbol(\"name\")  <\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><\/p>\n\n\n\n<p>Here, the valid identifier such as function, valid class, public member name, or library name can be used in place of&nbsp;<strong>name<\/strong>&nbsp;value.<a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><\/p>\n\n\n\n<ol><li>#radix&nbsp;&nbsp;<\/li><li class=\"\">#bar&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library foo_lib;     \n\/\/ libarary name can be a symbol     \n  \nclass Foo {           \n   \/\/ class name can be a symbol    \n   m1() {          \n      \/\/ method name can be a symbol   \n      print(\"Inside m1\");   \n   }   \n   m2() {   \n      print(\"Inside m2\");   \n   }   \n   m3() {   \n      print(\"Inside m3\");   \n   }   \n}  <\/code><\/pre>\n\n\n\n<p>In the above code, we have declared a class Foo in a library foo_lib. The class contains the methods m1, m2, and m3. We save the above file as foo.dart.<\/p>\n\n\n\n<p>Now, we are creating new file FooSymbol.dart and run the following code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">FoolSystem.dart<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'dart:core';   \nimport 'dart:mirrors';   \nimport 'Foo.dart';    \n  \nmain() {   \n   Symbol lib = new Symbol(\"foo_lib\");     \n   \/\/library name stored as Symbol   \n     \n   Symbol clsToSearch = new Symbol(\"Foo\");    \n   \/\/ class name stored as Symbol    \n     \n   if(checkIf_classAvailableInlibrary(lib, clsToSearch))    \n   \/\/ searches Foo class in foo_lib library   \n      print(\"class found..\");   \n}    \n     \nbool checkIf_classAvailableInlibrary(Symbol libraryName, Symbol className) {   \n   MirrorSystem mirrorSystem = currentMirrorSystem();   \n   LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);   \n        \n   if (libMirror != null) {   \n      print(\"Found Library\");   \n      print(\"checkng...class details..\");   \n      print(\"No of classes found is : ${libMirror.declarations.length}\");   \n      libMirror.declarations.forEach((s, d) => print(s));    \n           \n      if (libMirror.declarations.containsKey(className)) return true;   \n      return false;   \n   }   \n}  \n&lt;\/pre>&lt;\/div>  \n&lt;p>The above code will show the following output.&lt;\/p>  \n&lt;p>&lt;strong>Output:&lt;\/strong>&lt;\/p>  \n&lt;div class=\"codeblock\">&lt;pre>  \nFound Library  \ncheckng...class details..  \nNo of classes found is : 1  \nSymbol(\"Foo\") \/\/ Displays the class name  \nclass found..  \n&lt;\/pre>&lt;\/div>  \n&lt;p>&lt;strong>Example - 2 : Print the number of instance methods of class&lt;\/strong>&lt;\/p>  \n&lt;p>In the following example, The Dart provides predefine class &lt;strong>ClassMirror&lt;\/strong> which helps us to display the number of instance methods of class.&lt;\/p>  \n&lt;p>&lt;strong>Example -&lt;\/strong>&lt;\/p>  \n&lt;div class=\"codeblock\">&lt;textarea name=\"code\" class=\"java\">  \nimport 'dart:core';   \nimport 'dart:mirrors';   \nimport 'Foo.dart';    \n  \nmain() {   \n   Symbol lib = new Symbol(\"foo_lib\");   \n   Symbol clsToSearch = new Symbol(\"Foo\");    \n   reflect_InstanceMethods(lib, clsToSearch);   \n}    \nvoid reflect_InstanceMethods(Symbol libraryName, Symbol className) {   \n   MirrorSystem mirrorSystem = currentMirrorSystem();   \n   LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);   \n     \n   if (libMirror != null) {   \n      print(\"Found Library\");   \n      print(\"checkng...class details..\");   \n      print(\"No of classes found is : ${libMirror.declarations.length}\");   \n      libMirror.declarations.forEach((s, d) => print(s));    \n        \n      if (libMirror.declarations.containsKey(className)) print(\"found class\");  \n      ClassMirror classMirror = libMirror.declarations&#91;className];   \n        \n      print(\"No of instance methods found is ${classMirror.instanceMembers.length}\");  \n      classMirror.instanceMembers.forEach((s, v) => print(s));   \n   }   \n}     <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Found Library\ncheckng...class details..\nNo of classes found is : 1\nSymbol(\"Foo\")\nfound class\nNo of instance methods found is 8\nSymbol(\"==\")\nSymbol(\"hashCode\")\nSymbol(\"toString\")\nSymbol(\"noSuchMethod\")\nSymbol(\"runtimeType\")\nSymbol(\"m1\")\nSymbol(\"m2\")\nSymbol(\"m3\")\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Convert Symbol to String<\/h2>\n\n\n\n<p>We can convert the Dart symbol into the string by using a built-in class&nbsp;<strong>MirrorClass,<\/strong>&nbsp;which is provided by the dart:mirror package. Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-symbol#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'dart:mirrors';   \nvoid main(){   \n   Symbol lib = new Symbol(\"foo_lib\");   \n   String name_of_lib = MirrorSystem.getName(lib);   \n     \n   print(lib);   \n   print(name_of_lib);   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Symbol(\"foo_lib\")\nfoo_lib<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Symbol object is used to specify an operator or identifier declared in a Dart programming language. Generally, we do not need to use symbols while Dart programming, but they are helpful for APIs. It usually refers to identifiers by name, because identifier names can vary but not identifier symbols. Dart symbols are dynamic string name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[917],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3582"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3582"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3582\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}