How it starts ?
Like every other programming language, you have main function here, where the journey starts.
void main() {
print("Flutter Journey starts here");
}
What are the available DataTypes ?
a. Number (int or double) — int number = 10; || double number = 15.6;
b. String — e.g. String message = “I am $number years old”;
c. Booleans — True/ False;
d. List — e.g. List books = [“Chemistry”, “Physics”, “Maths”];
e. Maps — e.g. Map<int, String> dataMap = {01 : “KV”, 02: “VK”}
f. Runes
g. Symbols
h. “null” — Please note “null” is also object here
Difference between var and dynamic ?
var takes the type as type of first assigned value. Once assinged you can’t assign value of any other type.
var name = "Kapil";
name = 12; // here it will give compile time error.
dynamic — on the other hand and accept any value. This is equivalent to “Object” in java and “Any” in kotlin.
dynamic name = "KV";
print(name.runtimeType); // this will print - String
name = 123; // this works like charm.
print(name.runtimeType); // this will print - int
So always use var when you are aware of the dataType and sure that it will hold only one type of data. Use dynamic if type of data keeps on changing.
Access Modifiers ?
Unlike Java and any other programming language it doesn’t have private, public, protected etc. But there is way to make variables private by adding “_” as a prefix in variable.
String name = "KV"; // this is public
String _name = "VK"; // this is private
Functions :
Very similar to java.
Return Type => Function Name => Arguments
String getData(int value, String type) {
return "Hey mate!";
}
Making arguments optional :
Here you just need to add arguments under square braces to make them optional. Else it will give compile time error if you don’t pass all arguments.
void main() {
print(getData(11));
}
String getData(int value, [String? items]) {
return "Hey $items is $value";
}
------------------------------------
Console Output -> Hey null is 11
Making arguments non-positioned :
By default, dart expects arguments to be positioned. That means while calling methods or classes you have to pass aruments in order as metioned.
Below example doesn’t work and will give compile time issue.
void main() {
print(getData("here", 11));
}
String getData(int value, String items) {
return "Hey $items is $value";
}
------------------------------------
This will give compile time error, because "int" is expected as first argument
and "String" as second in above example.
To make it non-positional, you can use curly braces “{” and “}”. But this makes all arguments optional and non-positional. So either you have to add “required” keyword to make it mandatory or add “?” to let compiler know that it supports null value.
void main() {
print(getData(items : "here", value : 11));
}
String getData({required int value, String? items}) {
return "Hey $items is $value";
}
Classes and Objects :
“new” keyword is optional here. Classes have default constructor by default like in Java.
void main() {
User user1 = new User(name : "Kapil", age : 30); // new keyword is optional and can be removed.
}
// Type 1
class User {
String? name;
int? age;
User({String? name, int? age}) {
this.name = name;
this.age = age;
}
}
// Type 2 - more concised way
class User {
String? name;
int? age;
User({this.name, this.age});
}
final and late Keyword :
class User {
final String name;
int? age;
late final String appearance; // this is immutable but is not initially
bool _isAdult = true; // this is private and has default value, hence optional
User({required this.name, this.age});
}
Constructors :
Dart doesn’t support multiple constructors with same name even with different arguments. But there is a way to handle this
void main() {
User user1 = User(name: "Kapil", age: 30);
User user2 = User.withAppearance(name: "KV", age: 31, appearance: "tall");
}
class User {
final String name;
int? age;
late final String appearance;
User({required this.name, this.age});
User.withAppearance({required this.name, this.age, required this.appearance});
}
Here “User.withAppearance” is a Named constructor.
— — — — — — — — — — — — — That’s all folks — — — — — -— — — — — — — —
Lets catch up in flutter journey here — https://kapilvij.medium.com/learning-flutter-part-1-7239aac81bba