Skip to content

Instantly share code, notes, and snippets.

View nikli2009's full-sized avatar
🌏
Working from home

nikli2009

🌏
Working from home
View GitHub Profile
@nikli2009
nikli2009 / hello-world.dart
Last active July 3, 2019 06:20
Dart Basics
void main() {
print('Hello Dart');
}
@nikli2009
nikli2009 / variables.dart
Last active July 3, 2019 07:33
Dart Variables
void main() {
// Way 1
print('----Declare Way 1----');
var unlimited;
unlimited = 'unlimited';
print(unlimited);
// 'unlimited' => 1000.00, ALLOWED
unlimited = 1000.0;
print(unlimited);
@nikli2009
nikli2009 / const_final.dart
Last active July 3, 2019 08:18
Dart Const / Final
import 'dart:math';
void main() {
const String USER_NAME = 'nikk';
// USER_NAME = 'wenyan'; => Error
final String USER_ADDRESS = 'China';
// USER_ADDRESS = 'United States'; => Error
// ------------------------------------
@nikli2009
nikli2009 / class.dart
Last active July 4, 2019 10:12
Dart Class
void main() {
Car newMercedes = Car(brandName:'Benz', modelName:'G', currency:'USD', msrp:1000.0);
print('Car: ${newMercedes.brandName} ${newMercedes.modelName}');
print('Price: ${newMercedes.currency} ${newMercedes.msrp}');
print(''.padLeft(20, '-'));
Car newHonda = Car.shorter(brandName:'Honda', modelName:'Fit');
print('Car: ${newHonda.brandName} ${newHonda.modelName}');
@nikli2009
nikli2009 / optional_class.dart
Last active July 4, 2019 12:40
Dart Optional Named Class
void main() {
// provide only required parameter
FunnyText uselessWidget1 = FunnyText('hello');
// provide an empty named parameter
FunnyText uselessWidget2 = FunnyText('hello', style: TextStyle());
// provide a complete named parameter to override DEFAULT value, which is 20.0
FunnyText uselessWidget3 = FunnyText('hello', style: TextStyle(fontSize: 30.0));
uselessWidget1.printFontSize(); // 20
uselessWidget2.printFontSize(); // 20
@nikli2009
nikli2009 / safe_operators.dart
Created July 4, 2019 14:10
Dart Safe Operators
void main() {
// ? Operator
Coke myCoke = Coke(volume: 600);
// this is unsafe as anyone can change volume before printing.
// for example, myCoke = null
// then it gonna throw an error.
print(myCoke.volume); // 600
// so let's use ? as null check operator to ensure this won't happen
@nikli2009
nikli2009 / list.dart
Created July 4, 2019 16:26
Dart List
void main() {
List<String> usernames = ['lana', 'del', 'rey', 'hope', 'sandoval'];
// add => bottom
usernames.add('nikk');
print(usernames); //[lana, del, rey, hope, sandoval, nikk]
// add all
usernames.addAll(['wenyan', 'li']);
print(usernames); // [lana, del, rey, hope, sandoval, nikk, wenyan, li]
@nikli2009
nikli2009 / map.dart
Created July 5, 2019 11:29
Dart Map
void main() {
// handle data
// use Class is kind of overkill from Design Perspective
var user = {
'name': 'Nikk.Li',
'age': 28,
'height': 171.0,
'weight': 63,
'children': null
@nikli2009
nikli2009 / enum.dart
Created July 7, 2019 09:58
Dart Enum
void main() {
var orderType = OrderType.Domestic;
// ENUM item -> index
print(' '.padLeft(10,'-') + 'Enum index' + ' '.padRight(10,'-'));
print(orderType.index);
// ENUM item -> value
print(' '.padLeft(10,'-') + 'Enum value' + ' '.padRight(10,'-'));
print(orderType);
}
@nikli2009
nikli2009 / ternary_operator.dart
Created July 7, 2019 10:14
Dart Ternary Operator
void main() {
print(' '.padLeft(10,'-') + 'Ternary Operator' + ' '.padRight(10,'-'));
bool expression = true;
// Logic -> used in business logic of binary/simple strategy;
expression ? {
print(true)
} : {
print(false)