Skip to content

Instantly share code, notes, and snippets.

@alex-amenos
Last active December 18, 2020 12:11
Show Gist options
  • Save alex-amenos/63c85d3e6c3ac63638c383e67bb01ade to your computer and use it in GitHub Desktop.
Save alex-amenos/63c85d3e6c3ac63638c383e67bb01ade to your computer and use it in GitHub Desktop.
Dart language samples

Dart playground

Using https://dartpad.dartlang.org/

Basic Dart concepts

void main() {
  // Strings
  var name = "Jane";
  String surname = "Jones";
  println("Hello $name $surname!");
  println("First name of $name is ${name[0]}!");

  // Double
  double pi = 3.141592;
  println("Pi is $pi");

  // Int
  int start = 0;
  int max = 5;
  for (int i = start; i < max; i++) {
    println("number: $i");
  }

  // Boolean
  bool isEnable = true;
  if (isEnable) {
    println("Feature is enabled");
  } else {
    println("Feature is disabled");
  }

  // Lists
  List<int> numbersList = [1, 2, 3, 4, 5];
  println("Number list is $numbersList");

  List<List<int>> matrix = [
    [1],
    [2, 3, 4, 5]
  ];
  println("Matrix is $matrix");

  List<String> nameList = ["John", "Jane", "Noa"];
  nameList.add("Marcus");
  nameList[0] = "Maria";
  var iterator = nameList.iterator;
  while (iterator.moveNext()) {
    println("Name is ${iterator.current}");
  }

  // Map
  Map<String, dynamic> personMap = {"name": "Maria", "age": 32, "single": true};
  breakline();
  println("Person map $personMap");
  println("Person age ${personMap["age"]}");

  Map<int, String> people = {1: "Alex", 2: "Toni"};
  people.addAll({4: "Helen"});
  breakline();
  println("People $people");
  println("People 1 ${people[1]}");
  print(people[2]);
  
  // Functions
  breakline();
  println("Hello world!");
  println(buildStringWith(prefix: "> ", base: "Hello", sufix: "!"));
}

void breakline() => print("");

void println(String text) => print("$text\b");

String buildStringWith({String prefix, String base, String sufix}) {
  return "$prefix$base$sufix";
}
      

Classes

void main() {
  final superman = Hero("Superman", "fly");
  println(superman.toString());
}

class Hero {
  String name = "unnamed";
  String power = "undefined";

  Hero(this.name, this.power);

  String toString() => "name: $name\n" + "power: $power\n";
}

Parse JSON

  • Parse a JSON string.
  • Constructors with names.
import 'dart:convert';

void main() {
  final rawJson = '{ "name": "Superman", "power": "fly"}';
  Map parsedJson = json.decode(rawJson);
  
  final superman = new Hero.fromJson(rawJson);
  println(superman.toString());
  
  final supermanCopy = new Hero.fromMap(parsedJson);
  println(supermanCopy.toString());
}

class Hero {
  String name = "unnamed";
  String power = "undefined";

  Hero(this.name, this.power);
  
  Hero.fromJson(String rawJson) {
    var parsedJson = json.decode(rawJson);
    name = parsedJson["name"];
    power = parsedJson["power"];
  }
  
   Hero.fromMap(Map rawMap) {
    name = rawMap["name"];
    power = rawMap["power"];
  }

  String toString() => "name: $name\n" + "power: $power\n";
}

void println(dynamic value) => print("$value\n");

Getter and setter

void main() {
  final sqr = new Square();

  sqr.side = 5;
  println("Square area is ${sqr.area}");

  // Throw an error
  sqr.side = 0;
}

class Square {
  double _side; // private var

  set side(double value) {
    if (value <= 0) {
      throw ("Wrong side value (side >= 0)");
    } else {
      _side = value;
    }
  }

  double get area => _side * _side;
}

void println(dynamic value) => print("$value\n");

Abstract class

void main() {
  final dog = new Dog(legs: 4, tails: 1);

  dog.emitSound();
}

abstract class Animal {
  int legs;

  void emitSound();
}

class Dog implements Animal {
  @override
  int legs;
  int tails;

  Dog({this.legs, this.tails});

  @override
  void emitSound() => print("Guauuu!!!");
}

Extend

void main() {
  final superman = new Hero(8);
  superman.name = "Clark Kent";
  superman.print();
  println(superman.toString());
  
  final luthor = new Villain(6);
  luthor.name = "Lex Luthor";
  luthor.print();
  println(luthor.toString());
}

abstract class Character {
  String name = "unnamed";
  
  String toString() => "name: $name\n";
}

class Hero extends Character {
  int courage;
  
  Hero(this.courage);
    
  void print() => println("name: $name, courage: $courage\n");
}

class Villain extends Character {
  int evil;
  
  Villain(this.evil);
    
  void print() => println("name: $name, evil: $evil\n"); 
}

void println(dynamic value) => print("$value\n");

Mixins

Dart: What are mixins? by Romain Rastel

Future

void main() {
  println("Main start");
  
  helloWorld(5).then((data) {
    println(data);    
  });
  
  println("Main end");
}

Future<String> helloWorld(int delaySeconds) {
  return Future.delayed( new Duration( seconds: delaySeconds ), () {
    return 'Hello world!';
  });
}

void println(dynamic value) => print("$value\n");

Async & await

void main() async {
  println("Main start");

  String data = await helloWorld().then((data) {
    println(data);
  });

  println("Main end");
}

Future<String> helloWorld() {
  return Future.delayed(new Duration(seconds: 5), () {
    return 'Hello world!';
  });
}

void println(dynamic value) => print("$value\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment