Skip to content

Instantly share code, notes, and snippets.

@modernlabrat
Last active July 30, 2022 21:49
Show Gist options
  • Save modernlabrat/e4735d874606ec630213c5e2bc26836e to your computer and use it in GitHub Desktop.
Save modernlabrat/e4735d874606ec630213c5e2bc26836e to your computer and use it in GitHub Desktop.
Dart Basics
/*
* Dart Basics:
* 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
*
* Dart is an object-oriented programming language.
* Dart is camel-case, like Java
* Dart is strong-typed.
*
* Add type, or it will assume a dynamic type (accepts any value) Avoid!!
* Assign implicit types.
*
* Follows the semi-colon ruling like Java.
*
* Basic Data Types: String, int, double, num type allows both
* integer and doubles
*
* Comments: Single/Multi-Line
*
* Backlash - escape characters, enclosed string quotes, \\ escaped
* backlash.
*
*/
/*
* void functions return nothing.
* You cannot print void functionName() as it doesn't return anything.
* Single line functions do not use semi-colons.
* Use => for one line of code in the function body.
*
*/
int addNumbers(int num1, int num2) => num1 + num2; // A function or method that adds two integers, num1 and num2, passed into the parameters.
/*
* Create a class to define the blueprint of an object.
* Naming Convention for classes == Uppercase
*/
class Person {
/* variables in classes are sometimes called 'properties'
* variables in functions are called 'variables'
* immutable variables: final and const, replaces keyword var
* * * * can be prefixed like so - final String name = 'Kyra';
* final: needed for all widget properties in Flutter, widgets are
* immutable.
* const: used to create compile-time constants. no const arguments.
*/
String name; // decalred with String because there is not an initial value.
/* Strings can be single or double quotes.
* The operator plus is used to concatenate / interpolate strings.
* String properties: codeUnits, isEmpty, Length
* Some String methods: toLowerCase(), toUpperCase(), trim(),
* compareTo(), replaceAll(), split(), subString()
*/
int age; // since there is not initial value, use 'int' instead of 'var'
void greet() { // greet() function prints a personal greeting for a Person object.
print('Hi, I am ' + name + ' and I am ' + age.toString()); // use toString() for int, double, and float values.
}
}
void main() { // Dart Starts Here First!!
// use print() like Python or console.log() like Javascript
print(addNumbers(4,2)); // I am passing 3 and 4 to addNumbers() and calling the function.
/*
* Dart has 'type inference'
*
* To declare variables use the keyword 'var'
* To declare constant variables use the keyword 'final'
* Declare variables with an implicit type if there is not an inital
* value assigned.
*
*/
var nine = addNumbers(4, 5); // Store result of addNumbers(4,5) into an integer variable named nine
print(nine);
var ten = addNumbers(3,7);
var nineteen = nine + ten; // adding variables together
print(nineteen);
int two = addNumbers(1,1);
int three = addNumbers(1, two);
print(three);
// Creating an instance of the Person class.
var person1 = Person(); // Dart doesn't require the new keyword, although it can be used.
person1.age = 32; // Assigning values to person1 i.e age, name
person1.name = 'Kyra';
print('person1: ${person1.age}'); // ${} can be used to interpolate a value in a Dart expression.
print(person1.name);
person1.greet(); // Calling the greet function on the Person person1 instance.
/*
* assert() is for testing/debugging - terminates the code if the
* condition is FALSE. Assume the condition is true in the parenthesis.
*
*/
assert(two =='2');
// Lists
/*
* Lists are an ordered collection of data
* Elements are indexed from 0 to length-1
* Lists can have If and For loops nested inside.
* Lists can be created by the List.generate constructor, which takes two
* arguments: the numbers of elements and a callback function that
* generates each element based on its index.
*
* A callback function: a function to be run by another function in
* certain conditions and with certain arguments.
*
*/
var condition = two == 2;
var list1 = [
if(condition) print('two') // notice the lack of semicolons
else print('not two')
];
print(list1);
var list2 = [20,30,40]; // create a list
assert(list2[0] == 20); // get a list value by splicing the list with square brackets and the appropiate index value.
print(list2);
// add/remove items to/from a list
list2.add(50);
print(list2);
list2.remove(20);
print(list2);
list2.removeAt(1); // removing an element by its index.
print(list2);
var list3 = [60, 70];
// iterate over lists: for, map, forEach
for(var number in list2) { // for
print(number);
}
/*
* map and forEach are alternativs to the for in loop
* forEach similar to for with a callback type of void
*
*/
list2.forEach((number) {
print(number);
}); // note the syntax forEach()
/*
* .map creates an Iterable. Below the variable 'iter' is an Iterable,
* not a List, so use the toList method for appropiate List conversion.
*
*/
var iter = list2.map((number) => "#$number").toList(); // Note the added # symbol in the output.
print(iter);
/*
* Ternary operators are used to
* evaluate a single line
* condition.
*
*/
var condition2 = true; // execute a statement
condition2 ? print('when i\'m true') : print( 'when i\'m false');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment