This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:math' as math; | |
| class Point { | |
| final int x; | |
| final int y; | |
| const Point(this.x, this.y); | |
| } | |
| // class / abstract class can be used as mixin | |
| class LastPoint { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Dart is a lexically scoped language, which means that the scope of variables is determined statically, simply by the layout of the code. You can “follow the curly braces outwards” to see if a variable is in scope. | |
| // example | |
| bool topLevel = true; | |
| void main() { // | |
| var insideMain = true; | |
| void myFunction() { | |
| var insideFunction = true; | |
| void nestedFunction() { | |
| var insideNestedFunction = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //import 'package:meta/meta.dart'; | |
| String f1() => 'value'; // For functions that contain just one expression, you can use a shorthand syntax | |
| String f2() { // this function equivalent to f1 | |
| return 'value'; | |
| } | |
| /// Functions as first-class objects | |
| /// You can pass a function as a parameter to another function. For example and return function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| // ******* | |
| // Conditional statement | |
| // ******* | |
| // example: value mapping | |
| // '1' = 'yes' | |
| // '0' = 'no' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| String a; // null | |
| if (a == null) { | |
| a = 'Hello'; | |
| } | |
| // equivalent to | |
| a ??= 'Hello'; | |
| // equivalent to | |
| a = a ?? 'Hello'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| dynamic value = 1; | |
| assert(value is int); // True if the object has the specified type | |
| assert(value is! double); // False if the object has the specified type | |
| int intValue = value as int; | |
| print(intValue); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // If name of variable, function, class, method etc. stars with ‘_’ then it's visible only inside this file. | |
| // Example 1: | |
| String _str1 = 'Hello'; // private | |
| String str2 = "World"; // public | |
| // Example 2 | |
| void main() { | |
| final car = Car('Audi', 'A8'); | |
| print('model: ${car._model}'); // visible only inside of the file | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| // var | |
| var variable = 1; // equivalent to int a1 = 1, type calculated from right part | |
| variable = 2; // var can be mutated | |
| // final | |
| final imutableVariable = 'object 1'; // final is imutable | |
| final imutableVariable2 = 'object 1'; // will create additional instance of string 'object 1' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| /// Uninitialized variables have an initial value of null. Even variables with numeric types are initially null, because numbers—like everything else in Dart—are objects. | |
| int lineCount; | |
| assert(lineCount == null); // woesn't work in dartpad | |
| print(lineCount); | |
| // Note: Production code ignores the assert() call. During development, on the other hand, assert(condition) throws an exception if condition is false. For details, see Assert. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| // List | |
| List<int> list1 = [1, 2, 3]; // List<int> | |
| List<int> list3 = []; // Empty list | |
| List<String> list2 = ['', 'sdf', '']; | |
| int item0List1 = list1[0]; // read value | |
| list1[0] = 5; // set value for index = 0 | |
| list3 = [ |