Google Dart programming syntax and basic types tutorial

  • 2020-03-29 23:59:27
  • OfStack

1. Variable declaration

How to define variables


var name = 'Bob';

The initial value of the variable


int lineCount;
assert(lineCount == null); // Variables (even numbers) are initially null.

You can use var or you can specify the type directly.
Final, a variable defined as final, whose value cannot be changed


final name = 'Bob'; // Or: final String name = 'Bob';
name = 'Alice';     // ERROR

2. Basic types

string

Strings can use single or double quotes.


var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";


In the string, you can directly apply the value, ${expression}, if only one variable, you can remove {}

var s = 'string interpolation';
assert('Dart has $s, which is very handy.' ==
       'Dart has string interpolation, which is very handy.');
assert('That deserves all caps. ${s.toUpperCase()} is very handy!' ==
       'That deserves all caps. STRING INTERPOLATION is very handy!');


Multi-line strings are considered to be concatenated by default.

var s = 'String ''concatenation'
        " works even over line breaks.";
assert(s == 'String concatenation works even over line breaks.');

If you want to use a multi-line string, you can do this by using ''


var s1 = '''
You can create
multi-line strings like this one.
''';


Creates a string without escaping

var s = @"In a raw string, even n isn't special.";

StringBuffer, very similar to that in.net.


var sb = new StringBuffer();
sb.add("Use a StringBuffer ");
sb.addAll(["for ", "efficient ", "string ", "creation "]);
sb.add("if you are ").add("building lots of strings.");
var fullString = sb.toString();

digital

There are two main types, int and double, which inherit num

Conversion between Numbers and strings


// String -> int
var one = Math.parseInt("1");
assert(one == 1);
// String -> double
var onePointOne = Math.parseDouble("1.1");
assert(onePointOne == 1.1);
// int -> String
var oneAsString = 1.toString();
assert(oneAsString == "1");
// double -> String
var piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == "3.14");


Boolean type

Bool, unlike js, is false as long as it is not true.

Lists(can be used as an array)


var list = [1,2,3]; //Instantiate a list
list.add(4);       //Add an element 4

You can use for, for... In, foreach() to go through a list.


var list = [1,2,3];
for (final x in list) {
  print(x);
}


or


var list = [1,2,3];
list.forEach((element) => print(element));

Maps (dictionary type)


var gifts = {                         // A map literal
// Keys       Values
  "first"  : "partridge",
  "second" : "turtledoves",
  "fifth"  : "golden rings"};
gifts["third"] = "apple"; //Add a

Iterate using foreach


var gifts = {
  "first" : "partridge",
  "second": "turtledoves",
  "fifth" : "golden rings"};
gifts.forEach((k,v) => print('$k : $v'));

GetKeys () and getValues() methods


var gifts = {"first": "partridge", "second": "turtledoves"};
var values = gifts.getValues();
//Print partridge and turtledoves, but not necessarily in that order.
values.forEach((v) => print(v));


Related articles: