Skip to content

Instantly share code, notes, and snippets.

@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_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_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_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_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_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_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_exceptions.dart
Created January 24, 2020 09:51
dart_exceptions.dart
import 'dart:math' as math;
class MyError extends Error {}
class MyException implements Exception {
MyException([dynamic message]);
}
void checkValue(int value) {
switch(value) {
@AlexVegner
AlexVegner / dart_class.dart
Created January 24, 2020 13:12
dart_class.dart
/// class
class Pesron {
String name; // field
int age;
String description() {
// method
return '$name, age: $age';
}
}
@AlexVegner
AlexVegner / dart_extension.dart
Created January 24, 2020 13:46
dart_extension.dart
/// Example 1
/// Enum extension
enum NetworkState {
unknown,
connected,
disconnected,
}
/// NetworkStateExtension
extension NetworkStateExt on NetworkState {