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() { | |
| 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
| 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() { | |
| // ******* | |
| // 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
| //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
| /// 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 '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
| import 'dart:math' as math; | |
| class MyError extends Error {} | |
| class MyException implements Exception { | |
| MyException([dynamic message]); | |
| } | |
| void checkValue(int value) { | |
| switch(value) { |
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
| /// class | |
| class Pesron { | |
| String name; // field | |
| int age; | |
| String description() { | |
| // method | |
| return '$name, age: $age'; | |
| } | |
| } |
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
| /// Example 1 | |
| /// Enum extension | |
| enum NetworkState { | |
| unknown, | |
| connected, | |
| disconnected, | |
| } | |
| /// NetworkStateExtension | |
| extension NetworkStateExt on NetworkState { |