avatarConstantin Stan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5654

Abstract

-subst">concatenated</span>'</span>); }</pre></div><figure id="3436"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UvsBau7H7NkwWUygMeUHbQ.png"><figcaption>Type <b>bool</b></figcaption></figure><p id="f6d5">The <b>bool</b> type represents booleans.</p><p id="db10">It has only two values: <b>true</b> or <b>false</b></p><p id="f844">Conditions like <b>if</b>, <b>while</b> or <b>assert</b> can be used only with <b>bool</b>s.</p><p id="0d3b">Dart’s type safety means that we can’t use code like <code>if(<i>nonBooleanValue</i>)</code> or <code>assert(<i>nonBooleanValue</i>)</code>. Instead, explicitly check for values, like this:</p><div id="ea9c"><pre><span class="hljs-keyword">void</span> main() { <span class="hljs-built_in">String</span> emptyName = <span class="hljs-string">''</span>; <span class="hljs-keyword">if</span>(emptyName.isEmpty) { <span class="hljs-built_in">print</span>(<span class="hljs-string">'Like the name says, it is empty!'</span>); } <span class="hljs-built_in">String</span> nullName; <span class="hljs-keyword">if</span>(nullName == <span class="hljs-literal">null</span>) { <span class="hljs-built_in">print</span>(<span class="hljs-string">'Like the name says, it is null!'</span>); } }</pre></div><figure id="559a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*CKT1Vc6zDQNWEOnQcRdlJA.png"><figcaption>Type <b>List</b></figcaption></figure><p id="60f1"><b>List</b> is a commonly used collection type.</p><p id="c4b1">Arrays (for people coming from other programming languages) are List objects.</p><p id="e72a">In the example below, both a List literal and a List constructor are used. It is recommended to use literals when possible:</p><div id="8de4"><pre><span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> { <span class="hljs-comment">// the literal way</span> <span class="hljs-type">List</span> <span class="hljs-variable">oneWay</span> <span class="hljs-operator">=</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]; <span class="hljs-comment">// the constructor way</span> <span class="hljs-type">List</span> <span class="hljs-variable">orAnother</span> <span class="hljs-operator">=</span> List&lt;<span class="hljs-type">int</span>&gt;(<span class="hljs-number">3</span>); }</pre></div><figure id="65cc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Zomhpf2ZbEAXO--lfMGe2A.png"><figcaption>Type <b>Map</b></figcaption></figure><p id="eb95"><b>Map</b> is another commonly used collection type.</p><p id="4271">In the example bellow, both a Map literal and a Map constructor are used. It is recommended to use literals when possible:</p><div id="219a"><pre><span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// the literal way</span> <span class="hljs-title class_">Map</span> oneWay = {<span class="hljs-string">'a'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'b'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'c'</span>: <span class="hljs-number">2</span>}; <span class="hljs-comment">// the constructor way</span> <span class="hljs-title class_">Map</span> orAnother = <span class="hljs-title class_">Map</span>&lt;<span class="hljs-title class_">String</span>, int&gt;(); }</pre></div><figure id="4fcc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Pj5R0JY-1U8-_u7mJ88xcg.png"><figcaption>Type <b>Set</b></figcaption></figure><p id="17ce"><b>Set</b>s, in Dart, are unordered collections of unique items.</p><p id="770f">To create an empty set, use <code>{}</code> preceded by a type argument, or assign <code>{}</code> to a variable of type <b>Set</b>.</p><p id="c380">The syntax for <b>map</b> literals is similar to that for set literals. Because <b>Map</b> literals came first to Dart, <code>{}</code> defaults to this type. If you forget to add the type annotation on <code>{}</code> or you don't assign <code>{}</code> to a variable of type <b>Set</b>, then, by default, Dart creates an object of type <b>Map&lt;dynamic, dynamic&gt;</b>.</p><div id="0a7d"><pre><span class="hljs-keyword">void</span> main() { <span class="hljs-comment">// the literal way</span> <span class="hljs-built_in">Set</span> oneWay = {}; <span class="hljs-comment">// the constructor way</span> <span class="hljs-built_in">Set</span> orAnother = &lt;<span class="hljs-built_in">String</span>&gt;{}; <span class="hljs-built_in">Set</span> petTypes = {<span class="hljs-string">'cat'</span>, <span class="hljs-string">'dog'</span>, <span class="hljs-string">'bird'</span>, <span class="hljs-string">'reptile'</span>, <span class="hljs-string">'other'</span>}; <span class="hljs-built_in">print</span>(<span class="hljs-string">'There are <span class="hljs-subst">{petTypes.length}</span> pet types defined.'</span>); }</pre></div><figure id="0d56"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1k0xNbi09U_CqR02vFcNWw.png"><figcaption>Type <b>Symbol</b></figcaption></figure><p id="9c46"><b>Symbol</b>s represent an operator or identifier.</p><p id="3aeb">They can be created using the constructor <b>Symbol('</b>name'<b>)</b> or the symbol literal <b>#</b></p><p id="2193">Symbols are rarely used. They shine when building APIs that refer to identifiers by name because minification changes identifier names but not identifier symbols.</p><p id="187e">Symbols created with the same name are eq

Options

ual:</p><div id="1e4d"><pre><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span>()</span> { print(Symbol(<span class="hljs-string">'a'</span>) == <span class="hljs-meta">#a); </span> <span class="hljs-comment">// the above prints true</span> }</pre></div><figure id="1be0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Qj_uF2yzhoaPq3bIfnyHLA.png"><figcaption>Type <b>Runes</b></figcaption></figure><p id="9460"><b>Runes</b> are UTF-32 code points of String. To express 32-bit Unicode values in a string, we can use the form<b> <code>\u****</code></b>, were <code>****</code>is the four-digit hexadecimal value of the code point.</p><p id="c7a1">If the code point is larger than 4, we'll use the <code>{}</code> to wrap the digits ( <code>\u{*****}</code>).</p><div id="38a3"><pre><span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span>(<span class="hljs-params"></span>) { <span class="hljs-title class_">Runes</span> heart = <span class="hljs-title class_">Runes</span>(<span class="hljs-string">'\u2665'</span>); <span class="hljs-title class_">Runes</span> laugh = <span class="hljs-title class_">Runes</span>(<span class="hljs-string">'\u{1f600}'</span>); <span class="hljs-title function_">print</span>(<span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCodes</span>(heart) + <span class="hljs-string">' '</span> + <span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCodes</span>(laugh)); }</pre></div><p id="5692">The above prints: '♥ 😀'</p><p id="dc8b">Next to the above built-in types, we have the <b>enum</b> and the <b>dynamic</b> types.</p><p id="4d04">An enumerated type can be declared using the <b>enum</b> keyword and allows us to define a set of constant values in a type-safe way (usually used in <b>switch</b> statements). Each value has an <b>index </b>getter to return the zero-based position of that value. <b>values</b> gives us access to all values in an enum.</p><div id="8cab"><pre><span class="hljs-keyword">enum</span> Pet { cat, dog, bird, reptile, <span class="hljs-symbol">other</span> }</pre></div><div id="1fcc"><pre><span class="hljs-keyword">void</span> main() { <span class="hljs-built_in">print</span>(<span class="hljs-string">'<span class="hljs-subst">${Pet.values.length}</span> pet types'</span>); <span class="hljs-built_in">dynamic</span> myPet = Pet.cat; <span class="hljs-keyword">switch</span>(myPet) { <span class="hljs-keyword">case</span> Pet.cat: <span class="hljs-built_in">print</span>(<span class="hljs-string">'meow'</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> Pet.dog: <span class="hljs-built_in">print</span>(<span class="hljs-string">'woof'</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> Pet.bird: <span class="hljs-built_in">print</span>(<span class="hljs-string">'tweet'</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> Pet.reptile: <span class="hljs-built_in">print</span>(<span class="hljs-string">'ssSss'</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> Pet.other: <span class="hljs-built_in">print</span>(<span class="hljs-string">''</span>); <span class="hljs-keyword">break</span>; } }</pre></div><p id="ac55">The <b>dynamic</b> type is used when we don’t know (or care about) the type of an object.</p><p id="5b4d">The <b>is</b> operator can be used to check if the dynamic object is of some type (the <b>runtimeType</b> getter gives us access to the actual type).</p><p id="8889">Sometimes the <b>dynamic</b> type is confused with <b>Object</b> (which should be used when we want to indicate that all objects are accepted).</p><div id="9922"><pre><span class="hljs-keyword">void</span> main() { <span class="hljs-built_in">dynamic</span> value = <span class="hljs-keyword">false</span>; <span class="hljs-built_in">print</span>(value.runtimeType); value = <span class="hljs-string">'Dart'</span>; <span class="hljs-keyword">if</span>(value <span class="hljs-keyword">is</span> <span class="hljs-built_in">String</span>) { <span class="hljs-built_in">print</span>(value.runtimeType); } }</pre></div><p id="0095">All these built-in types are part of the Dart programming language and provide flexibility and options to type our objects.</p><figure id="92b2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*U0TZ48KC-P8jwJp340UCFw.png"><figcaption>Built-in data types of <b>Dart</b></figcaption></figure><p id="fce5">In the next part of the <a href="https://medium.com/tag/fluttering-dart/archive"><b>Fluttering Dart</b></a> series, we’ll delve into <a href="https://readmedium.com/fluttering-dart-b37110f4d1bf">functions</a>, another Dart fundamental needed for building robust Flutter apps.</p><div id="3515" class="link-block"> <a href="https://readmedium.com/fluttering-dart-b37110f4d1bf"> <div> <div> <h2>Fluttering Dart: Functions</h2> <div><h3>How to write, use and abuse functions in Dart</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*88Gz2J-Hje09d7geYn752A.jpeg)"></div> </div> </div> </a> </div><p id="f721">Tha(nk|t’)s all!</p></article></body>

Fluttering Dart

Fluttering Dart: Built-In Data Types

Dart’s nuts and bolts

Built-in data types of Dart

Flutter projects can use both platform-specific and cross-platform code. The latter is written in Dart, and, for building Flutter apps, some basic knowledge of Dart is required.

Fluttering Dart’s goal is to explore fundamental knowledge and unveil tips & tricks of the powerful programming language that brings Flutter to life.

In this first part, we’ll discover Dart’s built-in data types.

The code examples can be tried out, and played with, using DartPad.

Built-In Data Types

A data type is a specific kind of information that allows certain operations.

Type num (the supertype of int and double)

Numbers, in Dart, can be of 2 types:

  • integer - represent whole numbers no larger than 64 bits (depends on the platform)
  • 64-bit double-precision floating-point (IEEE 754 standard) — used to represent numbers with decimal places

Their supertype is type num.

All numbers are also objects and, each, have specific methods.

void main() {
  int x = 4;
  print(x.toRadixString(2) == '100');
  double y = 19.861004;
  print(y.toStringAsFixed(4) == '19.8610');
}

In the above example, we first define an int called x that gets the value 4 and then we make use of the int’s method toRadixString to get to x’s binary representation (as a String). We could’ve used, for example the hexadecimal base - 16 - instead of 2. The equality between the representation of 4 in the binary base is '100' is true, and that is what the print method will display.

Then we define a double called y and we give it also a value (19.8610). We then use the double’s built-in function toStringAsFixed to get the only 4 decimal places of y (also as a String). The equality is also satisfied and therefore the print method will display true again.

Type String

A String is a sequence of UTF-16 code units.

Strings are used to represent anything you can write or express as a statement, as well as individual characters. For special characters representation, you can check bellow for Runes.

To create a string, we can use single ' or double " quotes (it doesn’t matter which of these, as long as we’re consistent across our code).

String interpolation is built-in in Dart. Expressions can be embedded into strings using the form ${expression}, and are evaluated when we use them. If the expression is an identifier, then we can omit the {}:

void main() {
  String dartLang = 'Dart';
  String javaLang = 'Java';
  print('Saying that $dartLang is identical to $javaLang might be a bit too much... At least when we count the letters in their names we get ${dartLang.length} in both cases.');
}

Multi-line strings can be created using triple quotes (either single or double):

void main() {
  String multiLine = '''
This 
is
nice!
''';
  print('$multiLine');
}

String concatenation is done simply by placing string literals next to each other without the + sign:

void main() {
  String concatenated = 'This ''is ' '(kind ''of) '
'odd'
'...'; 
  print('$concatenated');
}
Type bool

The bool type represents booleans.

It has only two values: true or false

Conditions like if, while or assert can be used only with bools.

Dart’s type safety means that we can’t use code like if(nonBooleanValue) or assert(nonBooleanValue). Instead, explicitly check for values, like this:

void main() {
  String emptyName = ''; 
  if(emptyName.isEmpty) {
    print('Like the name says, it is empty!');
  }
  String nullName;
  if(nullName == null) {
    print('Like the name says, it is null!');
  }
}
Type List

List is a commonly used collection type.

Arrays (for people coming from other programming languages) are List objects.

In the example below, both a List literal and a List constructor are used. It is recommended to use literals when possible:

void main() {
  // the literal way
  List oneWay = [1,2,3]; 
  // the constructor way
  List orAnother = List<int>(3);
}
Type Map

Map is another commonly used collection type.

In the example bellow, both a Map literal and a Map constructor are used. It is recommended to use literals when possible:

void main() {
  // the literal way
  Map oneWay = {'a': 0, 'b': 1, 'c': 2}; 
  // the constructor way
  Map orAnother = Map<String, int>(); 
}
Type Set

Sets, in Dart, are unordered collections of unique items.

To create an empty set, use {} preceded by a type argument, or assign {} to a variable of type Set.

The syntax for map literals is similar to that for set literals. Because Map literals came first to Dart, {} defaults to this type. If you forget to add the type annotation on {} or you don't assign {} to a variable of type Set, then, by default, Dart creates an object of type Map<dynamic, dynamic>.

void main() {
  // the literal way
  Set oneWay = {};
  // the constructor way
  Set orAnother = <String>{};
  Set petTypes = {'cat', 'dog', 'bird', 'reptile', 'other'};
  print('There are ${petTypes.length} pet types defined.');
}
Type Symbol

Symbols represent an operator or identifier.

They can be created using the constructor Symbol('name') or the symbol literal #

Symbols are rarely used. They shine when building APIs that refer to identifiers by name because minification changes identifier names but not identifier symbols.

Symbols created with the same name are equal:

void main() {
  print(Symbol('a') == #a); 
  // the above prints true
}
Type Runes

Runes are UTF-32 code points of String. To express 32-bit Unicode values in a string, we can use the form \u****, were ****is the four-digit hexadecimal value of the code point.

If the code point is larger than 4, we'll use the {} to wrap the digits ( \u{*****}).

void main() {
  Runes heart = Runes('\u2665');
  Runes laugh = Runes('\u{1f600}');
  print(String.fromCharCodes(heart) + ' ' + String.fromCharCodes(laugh));
}

The above prints: '♥ 😀'

Next to the above built-in types, we have the enum and the dynamic types.

An enumerated type can be declared using the enum keyword and allows us to define a set of constant values in a type-safe way (usually used in switch statements). Each value has an index getter to return the zero-based position of that value. values gives us access to all values in an enum.

enum Pet { cat, dog, bird, reptile, other }
void main() {
  print('${Pet.values.length} pet types');
  dynamic myPet = Pet.cat;
  switch(myPet) {
    case Pet.cat:
      print('meow');
      break;
    case Pet.dog:
      print('woof');
      break;
    case Pet.bird: 
      print('tweet');
      break;
    case Pet.reptile: 
      print('ssSss');
      break;
    case Pet.other: 
      print('');
      break;
  }
}

The dynamic type is used when we don’t know (or care about) the type of an object.

The is operator can be used to check if the dynamic object is of some type (the runtimeType getter gives us access to the actual type).

Sometimes the dynamic type is confused with Object (which should be used when we want to indicate that all objects are accepted).

void main() {
  dynamic value = false;
  print(value.runtimeType);
  value = 'Dart';
  if(value is String) {
    print(value.runtimeType);
  }
}

All these built-in types are part of the Dart programming language and provide flexibility and options to type our objects.

Built-in data types of Dart

In the next part of the Fluttering Dart series, we’ll delve into functions, another Dart fundamental needed for building robust Flutter apps.

Tha(nk|t’)s all!

Dart
Flutter
Web Development
Programming
Fluttering Dart
Recommended from ReadMedium