- Build a pure flutter&Console application
- Understand similarities and differences between Dart and other languages
F1
> dart: create new project > simple console application
- Every single dart app has a
main method
, run as soon as the application starts.
F5
run an app
2.12.0
is the minimal Stable Dart/flutter sdk version includes Null safety supported in our app
#####pubspec.yaml
1
2
|
environment:
sdk: '>=2.12.0 <3.0.0'
|
analysis_options
set of lint rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//String
String myString = 'Hello world';
myString.contains('He'); // Returns 'true'
//Numbers
int myInteger = 5;
double myDouble = 5.5;
print(myInteger.toString());
print(myDouble.toString());
//No more larger num types like float/long...
//both int/double are inherited from 'num'
num myNumber = 5//5.5 is OK as well
//Boolean
bool myBool = false
//Dynamic type can be anything(better not to use much)
dynamic myDynamic = 5;
myDynamic = 'Hello';
myDynamic = true;
|
1
2
3
4
5
6
7
8
9
|
//Infer 'var' gonna be String
var myStringVar = 'initial';
myString = 'modified';
//myStringVar = 5;
//*Notice: Can't do this because infer is not same as dynamic, which makes type immutable once inferred
final myStringFinal = 'final value'
// myStringFinal = 'new value'
//*Notice: Can't do this because final variable can only be set once
|
1
2
3
4
5
6
7
|
//Should use explicit type annotation but not dynamic
//if the variable is not initialized immediately
dynamic myString;
myString = 'Init'
//Final variables must be initialized at the very beginning
final String myFinalString = 'Init'
|
?
: null-safe access operator checks null variables on compile time instead of on run time
- It's unnecessary to initialize a nullable variable with null explicitly because null is the default value
1
2
3
4
|
//avoid run-time errors, check null on compile time
String? nullable = null
//nullable.length; //compile error
print(nullable?.length); //correct, will print out "null"
|
!
: unsafe access operator use this when we know the variable won't be null for sure
1
2
3
|
//avoid run-time errors, check null on compile time
String? nullable = null
print(nullable!.length); //run-time error
|
-
mathematical operators
=,+,-,*,/,!,?
: notes /
returns a double
~
means cut off the remainder
1
2
3
|
int result;
double resultDouble = 5 / 2;
result = 5 ~/ 2; //=2; can't assign 5/2 to an int directly
|
++, --, +=, -=, *=, /=
: notes ~/=
is for integer
-
boolean calculator
-
string concatenation
1
2
3
|
String suffix = '-debug';
String myString = 'Application$suffix';//Application-debug
String myCalResult = 'result is: ${1 + 2 / 5 + 3}';//result is: 4.4
|
Syntax is basically same as Java