匿名函式
Swift
{ x in…
}
或
{ $0.doSomething() }
Dart
(int x) {
…
}
或寫成單⾏行行
(x) => doSomething(x);
18.
匿名函式
Swift
class MyClass {
var f: ((Int)->Int)?
}
var x = MyClass()
x.f = { $0 + 1 }
print("(x.f!(2))"
// Swift 也可以⽤用 typealias
Dart
// ⼀一定要⽤用 typedef
typedef int Call(int);
class MyClass {
Call f;
}
main() {
var x = new MyClass();
x.f = (i) => i + 1;
print(x.f(2));
}
App:第⼀一個 Widget
void main()=> runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: ‘My App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new HomePage(title: ‘Home Page’),
);
}
} // 然後我們就可以繼續寫 HomePage class