Last active
December 5, 2020 20:29
-
-
Save modernlabrat/1637fada8ddc8b4a67f8442b3ad86ed0 to your computer and use it in GitHub Desktop.
Dart Basics II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Dart Basics II: | |
* Written By: Kyra Samuel | |
* Coded By: Kyra Samuel | |
* Twitter/GitHub/Discord: | |
* @modernlabrat | |
* Sources: | |
* * * * * * Udemy - Flutter & Dart | |
* - The Completed Guide [2020 | |
* Edition] | |
* * * * * * O'Reilly - Programming | |
* Flutter | |
* * * * * * Introduction to Dart for | |
* Java Developers | |
* | |
* DartPad.dev | |
* | |
* Dart doesn't use the keywords | |
* public, private, or protected. | |
* | |
* Dart supports top-level functions. | |
* | |
*/ | |
class _APrivateClass { | |
/* | |
* I am a private class. | |
* I am only accessible from within | |
* the current package. | |
*/ | |
} | |
void main() { | |
var a = 4.2e2; // specify powers of 10 by using 'e' | |
print('powers of 10: ' + a.toString()); // toString on num subclasses for proper String concatenation | |
num _privateNum; // a private variable | |
_privateNum = 4; | |
var fourInt = _privateNum as int; // type casting using the keyword 'as' | |
/* | |
* type casting is used in Dart | |
* to cast a value to one of its | |
* subtypes i.e num => int, num | |
* => double | |
*/ | |
print('num to int: ' + fourInt.toString()); // need toString() | |
List<num>list = [1, 2, 3]; // List of type num | |
print('num list: $list'); | |
var numListToInt = list.cast<int>(); // use 'cast' to cast all values of List | |
print('numListToInt $numListToInt'); | |
var roundMe = 5.6; | |
print('roundMe.round(): ' + roundMe.round().toString()); | |
/* | |
* The Non-null selector, ??, | |
* operator is used to select | |
* the non-null value. | |
* | |
*/ | |
var nonNull = 10; // non-null value | |
int nullNull; // null value | |
var sinceNonNull = nonNull ?? 2; // nonNull has a value of 10, so sinceNonNull has a value of 10. | |
var sinceNull = nullNull ?? 3; // nullNull has a value of Null, so sinceNull has a value of 3. | |
print('sinceNonNull: ${sinceNonNull}'); | |
print('sinceNull: ${sinceNull}'); | |
var aSet = { // A Set is a linear collection of different values. | |
'Red', | |
'Blue', | |
'Green', | |
'Purple' | |
}; | |
print('aSet: $aSet'); | |
// remove and add elements to a Set | |
var set2 = aSet | |
..remove('Red') | |
..add('Yellow'); // note the syntax. | |
print('set2: $set2'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment