Photo by RetroSupply on Unsplash
Dash into Dart
Dart first before flutter. "Fluttering, Flapping with Flair"
Forewords: The core idea here, shall we reflect on the foundation of programming language, before the leap into the Flutter framework. Let's take a look at the primitive programming build blocks from the variables, and operators, to Type. Many credits to TutorialsPoint for the material, and inspiration from the content.
Dart Programming Language concepts
the following is based on the Dark SDK 2.18.6 at the time of writing & (Flutter 3.3.10) in DartPad https://dartpad.dev/. Here's a quick summarized code for our studies. Fancy trying it out? Copy and paste to DartPad into a void main()
function and explore.
//Loops & Labels
currentLoop:
for (int i = 0; i < 7; i++) {
print('hello ${i + 1}');
if (i==5) break currentLoop; //break means exit the loop
if(i==4) continue currentLoop; //skip current element
}
//types
var abc = 'Smith'; //abc can't be assigned to 1
//abc = 1; //this has compile error
dynamic dyn = 'Hello'; //dyn could be assigned to 1
dyn = 1;
print(dyn is int); //true, best for checking 'dynamic'
print(dyn is! String); //false
//prefer lowerCamelCase for constant names
/* https://dart.dev/guides/language/effective-dart/style */
final solidName = 'the rock'; //set once during runtime.
const heavyName = 'the boulder'; //compile time constant
//operators
double totalSum = 1 + 2 - 3 * 4 / 5 % 6;
final integerResult = 5 ~/ 2 ; // expect 2
final increased5 = ++totalSum;
final decreased4 = --totalSum;
//Print, Output
print("$solidName : $abc");
//=> the rock : Smith
print("$dyn & $heavyName is $increased5 vs $decreased4");
//=> 1 & the boulder is 1.6 vs 0.6000000000000001
//Number, properties, then methods
totalSum.sign;
integerResult.isEven;
print(totalSum.hashCode); //hashcode for numeric value?
print("$totalSum ${totalSum.ceil()}"); //method for ceiling
print("${(-4).abs()}");
//we do (-4) because the -negate function acts after the function
Here's a little break, before we resume with String, Boolean, List & Map, and of Dynamic Type. Personally, I think Dart is a very interesting programming language, much reminded me of the fun learning programming of Java in the older days.
Photo by Target icons created by Freepik - Flaticon
Look into the String & Boolean
// ---------- Strings ----------
var singleLine = 'hello world';
var multiLine = '''hello world
this is a message
from the far far galaxy ''';
var jointLine = 'hello in single line '
'I have a message to write';
print(singleLine); // hello world
print(multiLine); // print the following include the indent
/*
hello world
this is a message
from the far far galaxy */
print(jointLine); //split message but join as one
//print: hello in single line I have a message ...
print('The sum of 100+101 is ${100+101}');
var newline = singleLine.toUpperCase().toLowerCase().replaceAll('l','n');
//string are immutable, cannot be changed,
////new instance could be made for all the modification
print(singleLine);
print(newline); //prints `henno wornd`
// ---------- Boolean ----------
bool isModified = false;
bool hadBreakfast = true;
print(isModified || hadBreakfast); // return true
print(singleLine.isEmpty); //property, that return false
//example expect Uniform ID of length 5, 00010
print('10'.padLeft(5, '0')); //return 00010
Decision-Making with If-else, and Switch Cases
for additional reference, only the linting part, check out this one [Curly_braces_in_flow_control_structures]
// ---- Decision Making -------
var fruit = 'apple';
//linter - apply braces for all control statements
// if -- else
if(fruit == 'apple'){
print('red');
}
else if(fruit =='banana'){
print('yellow');
}
else{
print('I don\'t know');
}
fruit = 'kiwi';
// switch case
switch(fruit){
case 'apple':
print('red');
break;
case 'banana':
print('yellow');
break;
default:
print('I don\'t know');
}
List - with null-safety enabled, Map
//with null safety enabled
var fixedList = List<int>.filled(3, 0, growable: false );
var growableList = List<int>.filled(3, 1, growable: true );
print(fixedList); // prints [0,0,0]
print(growableList); //print [1,1,1]
growableList.add(5);
var keyValuePairs = {'apple':'red', 'banana':'yellow'};
keyValuePairs['orange']='orange';
print(keyValuePairs);
print(keyValuePairs.keys); //apple,banana, orange
//Dynamic Type - Dart is an optionally typed language
the beauty of dart here is that we don't have to loop. The print would show the primitive content of the list.
๐ Bonus: Fun with the String, Runes
//-------- Bonus ----------
String x = 'ABC aeroplane';
print(x.codeUnits); //prints the UTF-16 code,
//Upper and Lower case difference = A 65, a 97 = 32.
x.runes.forEach((_){
print(new String.fromCharCode(_));
});
//Runes access to the character codes
Runes input = new Runes('\u{1F600}'); //smiley ๐
print(new String.fromCharCodes(input));
Conclusion
This is merely some handpicked example from Dart, it's not exhaustive. In the future article, we may explore some additional syntax available in the Dart language.