Anonymous functions
Programming for All
calc(int b) {
int c = 1;
return () => print("The value is ${b + c++}");
}
void main() {
(a, b) {
print("Hello, from Anonymous: ${a + b}");
}(10, 20);
var f = calc(2);
f();
calc(10)();
f();
}
Class in Dart
We can use class in dart like this.
class Greeter {
var greeting;
var _dragon;
sayKnight() {
return "$greeting ${this.dragon}";
}
get dragon => _dragon;
set dragon(value) => _dragon = value;
}
main() {
var greeter = new Greeter();
greeter.greeting = "U must win";
greeter.dragon = "General Nagar";
print(greeter.sayKnight());
}
class Welcomer {
printGreeting() => print("Hello ${dragon}");
var dragon;
}
class Greeter implements Welcomer {
printGreeting () => print("Greetings ${dragon}");
var dragon;
}
void sayKnight(Welcome welcomer) {
welcomer.printGreeting();
}
//Main functions
main() {
var welcomer = new Welcomer();
welcomer.name = "General Dragon";
sayKnight(welcomer);
var greeter = new Greeter();
greeter.name = "General Dragon Knight";
sayKnight(greeter);
}
Closure in Dart
calc(int b) {
int c = 1;
return () => print("The Value is ${ b + c++}");
}
void main() {
var x = (a, b) {
print("Hello, from Closure: {a + b}");
};
}
//Declare function using function shorthand.
Single line function
String sayHello(name) => "Hello $name";
This is only use for Comments //
main() {
var myFunc = sayHello; //Assign function in variable
print(myFunc("World")); //Invoke function stored in var
//Define Anonymous function
var mySumFunc = (a, b) {
return a+b;
};
//Invoke or Call Anonymous function
var c = mySumFunc(1, 2);
print(c);
}
Function
int add(int x, int y) => x + y;
int sub(int x, int y) => x - y;
choose(bool op) {
if (op == true) {
return add;
} else {
return sub;
}
}
void main() {
var result = choose(true);
print("Resuly is ${result(40, 50)}");
}
HTML in Dart
For using html in dart programming. We must import first to use in flutter.
import ‘dart:html’;
import 'dart:html';
void main() {
var button = new Element.tag("button");
button.text = "Click me";
button.onClick.map((event) {
List buttonList = querySelectorAll("button");
window.alert("There is ${buttonList.length} button");
});
document.body.children.add(button);
}
Sharing is Caring ( ^ _ > )
Wow : )
Thanks for sharing
Thanks
❤❤❤
Thanks.
Great!
Amazing! Gracias🐬🐬