Skip to content

Instantly share code, notes, and snippets.

@AlexVegner
AlexVegner / dart_mixin.dart
Created January 24, 2020 09:05
dart_mixin.dart
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 {
@AlexVegner
AlexVegner / dart_lexical_scope​.dart
Created January 24, 2020 08:37
dart_lexical_scope​.dart
/// 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;
@AlexVegner
AlexVegner / dart_functions.dart
Created January 24, 2020 08:33
dart_functions.dart
//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
@AlexVegner
AlexVegner / dart_control_flow_statements.dart
Last active January 23, 2020 17:40
dart_control_flow_statements.dart
void main() {
// *******
// Conditional statement
// *******
// example: value mapping
// '1' = 'yes'
// '0' = 'no'
@AlexVegner
AlexVegner / dart_null_check_operators​.dart
Created January 23, 2020 16:42
dart_null_check_operators​.dart
void main() {
String a; // null
if (a == null) {
a = 'Hello';
}
// equivalent to
a ??= 'Hello';
// equivalent to
a = a ?? 'Hello';
@AlexVegner
AlexVegner / dart_type_test_operators​.dart
Last active January 23, 2020 16:31
dart_type_test_operators​.dart
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);
}
@AlexVegner
AlexVegner / dart_public_private.dart
Last active January 23, 2020 16:24
dart_public_private.dart
// 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
}
@AlexVegner
AlexVegner / dart_var_final_const.dart
Created January 22, 2020 16:07
dart_var_final_const.dart
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'
@AlexVegner
AlexVegner / dart_default_value.dart
Created January 22, 2020 15:50
dart_default_value.dart
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.
}
@AlexVegner
AlexVegner / dart_list_set_map.dart
Last active January 22, 2020 15:39
dart_list_set_map.dart
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 = [