You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mixin: used to inject extra utility properties & methods
Mixinは , で区切って複数追加できるよ
mixinAgility {
var speed =10;
voidsitDown() {
print('Sitting down...');
}
}
classMammal {
voidbreathe() {
print('Breathe in... breathe out...');
}
}
classPersonextendsMammal {
finalString name; // 'final' suggests immutable after instantiationint age;
int _score; // '_'を付けるとPrivateになる=外部からアクセス不可。// ConstructorPerson({
@requirethis.name,
this.age =30,
});
// Special ConstructorPerson.veryOld(this.name) {
age =60;
}
// Overwriting methods...// @overwrite// void breathe() { ... }voidaddScore(score) {
_score += score
}
voidgreet() {
print('Hi, I am '+ name +' and I am '+ age.toString() +' years old!');
}
// getterStringget score => _score;
// setter -> eg) p1.score = 12;setscore(int score) {
_score = score;
}
}
voidmain() {
var p1 =Person(age:40, name:'Manu');
var p2 =Person.veryOld('John');
p1.breathe();
p1.greet();
p2.greet();
}
Regex
var urlPattern = r"(https?|ftp)://([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:,.;]*)?";
var result = new RegExp(urlPattern, caseSensitive: false).firstMatch('https://www.google.com');
HTTP Request
use http package
final url =Uri.parse('https://flutter-update.firebaseio.com/products.json');
http.post(url, body: json.encode({
'name': ...,
'age': ...,
...
})).then((res) {
final data = joson.decode(res.body);
// do something...
});
or
final url = Uri.https('flutter-update.firebaseio.com', '/products.json');
http.post(url, ...);
async & await を使うと…
Future<void> fetchProducts() async {
const url = '....';
try {
final res = await http.get(url);
print(res.body);
// do something...
} catch (err) {
throw(err);
}
}
長い res をprintしたい時は、、、
void printWrapped(String text) {
final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
pattern.allMatches(text).forEach((match) => print(match.group(0)));
}
printWrapped(res);
classMyWidgetextendsStatefulWidget {
// this widget will be re-created when input data changes...@overrideState<MyWidget> createState() =>_MyWidgetState();
}
class_MyWidgetStateextendsState<MyWidget> {
// this widget won't be re-created,// thus the state variabes here win't be reset.int _totalScore =0;
void _addTotalScore (int score) {
// force re-render the widget when state is updatedsetState(() => _totalScore += score);
}
Widgetbuild(BuildContext context) {
return ....
}
}