288 lines
No EOL
6.2 KiB
Markdown
288 lines
No EOL
6.2 KiB
Markdown
Basics 3 Classes
|
|
|
|
**Classes and Objects**
|
|
- in dart there is no constructor overloading so instead named constructor have to be used.
|
|
```dart
|
|
class Point {
|
|
int? x;
|
|
int? y;
|
|
|
|
// Default constructor
|
|
Point() {
|
|
x = 0;
|
|
y = 0;
|
|
}
|
|
|
|
// named constructor
|
|
Point.paramed(int x, int y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
// String interpolation
|
|
void printData() => print('$x + $y');
|
|
|
|
// functions
|
|
final void Function() onPressed;
|
|
}
|
|
|
|
void main() {
|
|
Point p = Point();
|
|
print(p.x);
|
|
|
|
Point p2 = Point.paramed(2, 3);
|
|
print(p2.x);
|
|
|
|
p.printData();
|
|
}
|
|
```
|
|
|
|
|
|
**Classes and enums**
|
|
- Intead of initializing data members inside block of constructor we can define them in initializer list too as it improves performance too
|
|
- we cant use some operators bcz of some null safety measures introdued in dart so use like this `assert(x!=null && x > 0)`
|
|
- For Getter and setters , we have keywords too
|
|
```dart
|
|
class Point {
|
|
int? x;
|
|
int? y;
|
|
|
|
// Default constructor
|
|
Point() {
|
|
x = 0;
|
|
y = 0;
|
|
}
|
|
|
|
// named constructor
|
|
Point.paramed(int x, int y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
// or
|
|
Point.paramed2(this.x, this.y);
|
|
// or
|
|
|
|
Point.paramed3(this.x, this.y) : assert(x != null && x > 0) {
|
|
printData();
|
|
}
|
|
|
|
Point.fromMap(Map<String, int> map) {
|
|
this.x = map['x'];
|
|
this.y = map['y'];
|
|
}
|
|
|
|
Point.fromMap2(Map<String, int> map)
|
|
: x = map['x'],
|
|
y = map['y'] {
|
|
printData();
|
|
}
|
|
|
|
// String interpolation
|
|
void printData() => print('$x + $y');
|
|
|
|
void printData2() {
|
|
print('$x - $y');
|
|
}
|
|
|
|
// getters
|
|
int? get pointX => this.x;
|
|
int? get pointY => this.y;
|
|
|
|
Map<String, int?> get getMap => <String, int?>{
|
|
'x': this.x,
|
|
'y': this.y,
|
|
};
|
|
|
|
// setters
|
|
set setX(int x) => this.x = x;
|
|
}
|
|
|
|
void main() {
|
|
Point p = Point();
|
|
print(p.x);
|
|
|
|
Point p2 = Point.paramed(2, 3);
|
|
print(p2.x);
|
|
|
|
p.printData();
|
|
p.printData2();
|
|
|
|
Point p3 = Point.fromMap(<String, int>{
|
|
'x': 10,
|
|
'y': 20,
|
|
});
|
|
|
|
p3.printData();
|
|
|
|
Point p4 = Point.fromMap2(<String, int>{
|
|
'x': 10,
|
|
'y': 20,
|
|
});
|
|
|
|
print(p3.pointX);
|
|
print(p3.pointY);
|
|
print(p3.getMap);
|
|
|
|
p.setX = 500;
|
|
p.printData();
|
|
}
|
|
```
|
|
Enums
|
|
- Enum is special variable which contains fixed number of constant values
|
|
```dart
|
|
enum PaymentStatus { notPaid,paid };
|
|
|
|
void main() {
|
|
final status = PaymentStatus.notPaid;
|
|
|
|
switch (status) {
|
|
case PaymentStatus.notPaid:
|
|
print(1);
|
|
break;
|
|
default:
|
|
8;
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
|
|
**Exceptional Handling**
|
|
- Infinity is a constant which is of double type
|
|
- To avoid typecasting automatically use tilt symbol before operator. eg : `var c2 = a ~/ b`
|
|
- Multiple catch can be used too for different types of exception too using `on <ExceptionName>`
|
|
```dart
|
|
// Custom Exception
|
|
class AgeException implements Exception {
|
|
String errMsg() => 'Age cannot be less than zero';
|
|
}
|
|
|
|
void main() {
|
|
print('programming knowledge');
|
|
|
|
try {
|
|
var a = 10;
|
|
var b = 0;
|
|
var c1 = a / b; // Output : Infinity
|
|
// var c2 = a ~/
|
|
// b; // Will give Error for unhandled exception : IntegerDivisionByZeroException
|
|
print('c1: $c1');
|
|
// print('c2: $c2');
|
|
|
|
var s = [1, 2, 3, 4];
|
|
print(s[2]);
|
|
|
|
var age = -10;
|
|
if (age < 0) {
|
|
throw AgeException();
|
|
}
|
|
} on AgeException catch (e) {
|
|
print(e.errMsg());
|
|
} on IntegerDivisionByZeroException catch (e) {
|
|
print('Division of zero isnt possible: ${e.message}');
|
|
} catch (e) {
|
|
print('Another catch block');
|
|
} finally {
|
|
print('always executed');
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
|
|
Async and await
|
|
- Future is just like promise in javascript and contains result of asyncronous operation which have 2 states ie completed and uncompleted. and if completed then its data is either some value or error.
|
|
```dart
|
|
Future<void> fetchUserData() {
|
|
return Future.delayed(Duration(seconds: 2), () => print('Data'));
|
|
}
|
|
|
|
void main() {
|
|
fetchUserData();
|
|
print('Fetching user data');
|
|
}
|
|
```
|
|
- async functions always returns Future datatype. async keyword will always be between closing parenthesis and opening curly braces
|
|
- If we dont use await or then we will get instance of the future not the result. if we use then it will assign callback for the result and switch to next line and if we use await then it will wait for result then move to next line.
|
|
```dart
|
|
Future<String> fetchUserData() {
|
|
return Future.delayed(Duration(seconds: 2), () => 'Data');
|
|
}
|
|
|
|
Future<String> fetchUserData2() {
|
|
return Future.delayed(Duration(seconds: 4), () => 'Data2');
|
|
}
|
|
|
|
Future<String> userMsg() async {
|
|
var data = await fetchUserData();
|
|
return '$data';
|
|
}
|
|
|
|
Future<String> userMsg2() async {
|
|
var data = await fetchUserData2();
|
|
return '$data';
|
|
}
|
|
|
|
void main() async {
|
|
print('Fetching');
|
|
|
|
// total time will be of 6s
|
|
print(await userMsg());
|
|
print(await userMsg2());
|
|
|
|
// total time will be of 4s
|
|
userMsg().then((value) => print(value));
|
|
userMsg2().then((value) => print(value));
|
|
|
|
// their output will be "Instance of 'Future<String>' "
|
|
print(userMsg());
|
|
print(userMsg2());
|
|
}
|
|
```
|
|
|
|
**Functions**
|
|
In Dart, `function` (lowercase 'f') refers to a specific, named block of code that performs a task. `Function` (uppercase 'F') refers to the general *type* of functions. Think of it like the difference between a specific car (e.g., a "Honda Civic") and the general concept of a "car."
|
|
|
|
Here's a breakdown:
|
|
|
|
**1. `function` (lowercase):**
|
|
|
|
* Represents a named, callable block of code.
|
|
* Used when defining or calling a specific function.
|
|
|
|
```dart
|
|
// Defining a function called 'myFunction'
|
|
void myFunction() {
|
|
print('Hello from myFunction');
|
|
}
|
|
|
|
// Calling the function
|
|
myFunction(); // Output: Hello from myFunction
|
|
```
|
|
|
|
**2. `Function` (uppercase):**
|
|
|
|
* Represents the function type itself.
|
|
* Used to declare variables, parameters, or return types that can hold any function, regardless of its specific signature (parameters and return type).
|
|
|
|
```dart
|
|
// Declaring a variable that can hold any function
|
|
Function myCallback;
|
|
|
|
// Assigning a function to the variable
|
|
myCallback = myFunction; // Assigning the 'myFunction' function
|
|
```
|
|
|
|
|
|
Regex
|
|
```dart
|
|
import 'dart:core';
|
|
|
|
void main() {
|
|
final exp=RegExp(r'/(.+)\..+$');
|
|
print(exp.firstMatch('a/jk.mp')!.group(1)); // jk
|
|
print(exp.firstMatch('a/jk.mp')); // true
|
|
}
|
|
|
|
``` |