Skip to content

Instantly share code, notes, and snippets.

View tailorvj's full-sized avatar
🤠
Yes, it's me

Tailor VJ tailorvj

🤠
Yes, it's me
View GitHub Profile
@tailorvj
tailorvj / dart_set_example.dart
Created March 25, 2020 19:22
Dart Set type example
main() {
Set<String> names = {'Rita', 'Alex', 'Sam', 'Eva'};
var persons = <String>{}; //inferred type Set
print('persons: $persons');
print('persons.length is ${persons.length}');
print('persons Type is ${persons.runtimeType}\n');
print('names: $names');
print('names.length is ${names.length}');
@tailorvj
tailorvj / dart_set_methods_example_a.dart
Created March 25, 2020 21:10
Dart Set .contains() .forEach() and .clear() methods
main() {
Set<String> nameSet = {'Rita', 'Alex', 'Sam', 'Eva'};
print('nameSet.contains(\'Sam\'): ${nameSet.contains('Sam')}\n');
nameSet.forEach((element){
print('element: $element');
});
nameSet.clear();
@tailorvj
tailorvj / dart_Set_toList_List_toSet_example.dart
Created March 25, 2020 21:24
Dart: Convert Set to List and back again using Set.toList() and List.toSet() methods
main() {
Set<String> nameSet = {'Rita', 'Alex', 'Sam', 'Eva'};
print('nameSet: $nameSet\n'); //{Rita, Alex, Sam, Eva}
List<String> nameList = nameSet.toList();
print('nameList: $nameList\n'); //[Rita, Alex, Sam, Eva]
Set<String> nameSet2 = nameList.toSet();
print('nameSet2: $nameSet2\n'); //{Rita, Alex, Sam, Eva}
}
@tailorvj
tailorvj / dart_operations_between_sets.dart
Created March 25, 2020 22:00
Dart operations between 2 Sets
main() {
var dividesBy2 = <int>{2,4,6,8,10,12,14,16,18,20};
var dividesBy3 = <int>{3,6,9,12,15,18,21};
var dividesBy6 = dividesBy2.intersection(dividesBy3);
print('dividesBy6: $dividesBy6\n');
var doesntDivideBy6 = dividesBy2.difference(dividesBy3);
print('doesntDivideBy6: $doesntDivideBy6\n');
@tailorvj
tailorvj / dart_list_iterable_collection.dart
Last active April 7, 2020 06:40
Dart: Lists are Iterable Collections
main() {
var list = ['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon'];
for (var item in list){
print('${list.indexOf(item)}: $item');
}
}
@tailorvj
tailorvj / flutter_basic_app_stateless_widget_main.dart
Created March 31, 2020 10:42
The most basic Flutter app in the world
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
@tailorvj
tailorvj / iterating_through_a_Map.dart
Created April 7, 2020 08:05
Dart example: Iterating through a Map
void main(){
Map<String, int> scores = {'Bob': 300};
print('Initial state of the scores Map: $scores');
for (var key in ['Bob', 'Rohan', 'Sophena']) {
scores.putIfAbsent(key, () => key.length);
}
for (MapEntry entry in scores.entries){
print('scores[\'${entry.key}\']: ${entry.value}');
@tailorvj
tailorvj / combine_maps_using_spread_operator.dart
Created April 7, 2020 08:53
Dart example: combining 2 Map objects with the spread operator
void main(){
Map map1 = {1: 'one', 2: 'two'};
Map map2 = {3: 'three', 4: 'four'};
Map map3 = {...map1, ...map2};
print('map1: $map1');
print('map2: $map2');
print('map3: $map3');
}
@tailorvj
tailorvj / simple_class_example.dart
Last active April 7, 2020 15:37
A simple Dart class example
void main(){
FullName myName = FullName('Tailor', 'VJ');
print(myName);
}
class FullName{
String firstName;
String lastName;
FullName(this.firstName, this.lastName);
String toString(){
@tailorvj
tailorvj / sort_list_of_objects.dart
Last active November 19, 2020 12:30
Example: sorting a list of objects in Dart
void main() {
List<FullName> fullNamesList = [FullName('Abe','Lincoln'), FullName('Babe', 'Ruth'), FullName('Carry', 'Bradshaw'),];
print ('fullNamesList:\n${fullNamesList.toString()}\n');
fullNamesList.sort((a,b) => a.lastName.compareTo(b.lastName));
print ('fullNamesList after sort by lastName:\n$fullNamesList\n');
fullNamesList.sort((a,b) => a.firstName.compareTo(b.firstName));
print ('fullNamesList after sort by firstName:\n$fullNamesList\n');
}