Skip to content

Instantly share code, notes, and snippets.

@vemarav
Created May 1, 2022 03:45
Show Gist options
  • Save vemarav/d01d92479a4d877d7f781e2f21ab7ddf to your computer and use it in GitHub Desktop.
Save vemarav/d01d92479a4d877d7f781e2f21ab7ddf to your computer and use it in GitHub Desktop.
introduction to dart
// import 'dart:async';
// import 'dart:math';
// every dart program starts with main function
void main() {
// basic data types
// 1. bool
// 2. int
// 3. double
// 3. String
// 4. dynamic
// 5. var
// 6. late
// if you don't specify type, dart uses type inference
// var hours = 24;
// print(hours);
// this will bypass type inference
// dynamic name = 'dart';
// print(name);
// functions
// String greet(String name) {
// String decorate() => '## $name ##'; // nested function
// return 'Hello, ${decorate()}';
// }
// print(greet('Function'));
// dynamic orphan = () {
// return 'anonymous function';
// };
// print(orphan());
// keyword late
// int getSum() {
// print('expensive function');
// return 0;
// }
// late int sum = getSum();
// sum;
// Objects act as data type
// 1. Map
// 2. List
// 3. Set
// 4. Queue
// 5. enum
// Map<String, int> map = {'age': 27, 'height': 175};
// print(map);
// List<int> numbers = [1,2, 3, 3, 5, 7, 8, 8, 9];
// print(numbers.asMap().keys.toList());
// Set set = Set.from(numbers);
// Set.from is a named constructor we will see later in classes and objects
// print(set);
// enum State { inProgress, success, failed }
classes();
}
// classes --------------------------------------
// class Car {
// final String price;
// final String engine;
// String color;
// Car(this.price, this.engine, this.color);
// static Tata tata() {
// return Tata();
// }
// factory Car.buildTata() { // similar to static function, but must return same class type
// return Tata();
// }
// String details() {
// return "Name\tPrice\tEngine\tColor\n--\t--\t--\n$runtimeType\t$price\t$engine\t$color";
// }
// }
// class Tata extends Car {
// Tata() : super('15L', '1.5L', 'White');
// }
// abstract classes -------------------------
// abstract class Car {
// // abstract classes can't be instantiated
// // useful to avoid boilerplate code, etc.
// final String price;
// final String engine;
// final String color;
// Car(this.price, this.engine, this.color);
// String get details => "Name\tPrice\tEngine\tColor\n--\t--\t--\n$runtimeType\t$price\t$engine\t$color";
// }
// class Tata extends Car {
// Tata() : super('10L', '1.5L', 'Silver');
// }
// interface classes ---------------------------
// class Car {
// final String price;
// final String engine;
// String color;
// Car(this.price, this.engine, this.color);
// static Tata tata() {
// return Tata();
// }
// }
// class Details {
// // interface is like a blueprint for it's subclasses
// String details() {
// throw UnimplementedError('function `details` is not defined for $runtimeType');
// }
// }
// class Tata extends Car implements Details {
// Tata() : super('10L', '1.5L', 'Silver');
// @override
// String details() {
// return "Name\tPrice\tEngine\tColor\n--\t--\t--\n$runtimeType\t$price\t$engine\t$color";
// }
// }
// mixin classes -----------------------------
// dart doesn't support multiple inheritance to avoid Deadly Diamond of Death (DDD)
// https://en.wikipedia.org/wiki/Multiple_inheritance#:~:text=The%20%22diamond%20problem%22%20(sometimes,from%20both%20B%20and%20C.
// that's where mixin come into play 😊
// and dart supports multiple mixin
// class Point {
// // (x,y) coordinates
// late int x; // late tells compiler don't worry you will get it when you need it
// late int y;
// double distanceFrom(Point other) {
// int dx = other.x - x;
// int dy = other.y - y;
// return sqrt(dx * dx + dy * dy);
// }
// }
// class Point2 {
// // (x,y) coordinates
// late int x; // late tells compiler don't worry you will get it when you need it
// late int y;
// double distanceFrom(Point other) {
// print('😛');
// return 0;
// }
// }
// class Perimeter {
// late int height;
// late int width;
// int get area => 2 * height * width;
// }
// // if two mixin's have same methods the latter wins
// class Rectangle extends Perimeter with Point, Point2 {}
// generic types ------------------
// class StateController<State> { // State is one of type {int, List, CustomType}
// State _state;
// StateController(this._state);
// State get state => _state;
// void modify(State newState) {
// _state = newState;
// }
// }
// class Todo {
// final int id;
// final String desc;
// Todo(this.id, this.desc);
// @override
// String toString() {
// return "\nid\tdesc\n$id\t$desc\n";
// }
// }
void classes() {
// class example ---------------
// Car tata = Car('10L', '1.5L', 'RoyalBlue');
// print(tata.details());
// Car sTata = Car.tata();
// print('\nusing static function\n${sTata.details()}');
// Car fTata = Car.buildTata();
// print('\nusing factory function\n${fTata.details()}');
// abstract classes example ---------------
// Car tata = Tata();
// print(tata.details);
// interface example ---------------
// Tata tata = Tata();
// print(tata.details());
// mixin example --------------
// Point origin = Point()
// ..x = 0
// ..y = 0; // cascade notation
// Rectangle rect = Rectangle()
// ..x=10
// ..y=10
// ..height=40
// ..width=40;
// print(rect.distanceFrom(origin)); // call to mixin method
// print(rect.area);
// generic types
// print('intController ------------');
// StateController<int> intController = StateController<int>(0);
// print(intController.state);
// intController.modify(5);
// print(intController.state);
// print('\nstringController ------------');
// StateController<List<String>> stringController = StateController<List<String>>([]);
// print(stringController.state);
// stringController.modify(['Welcome to dart types']);
// print(stringController.state);
// print('\ntodoController ------------');
// StateController<Todo> todoController = StateController<Todo>(Todo(0, ''));
// print(todoController.state);
// todoController.modify(Todo(1, 'done with dart'));
// print(todoController.state);
// StateController<List<Todo>> todosController = StateController<List<Todo>>([]);
// print("\n${todosController.state}");
// todosController.modify([
// Todo(1, 'entry 1'),
// Todo(2, 'entry 2'),
// Todo(3, 'entry 3'),
// ]);
// print("\n${todosController.state}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment