Skip to Content
Dart📘 Ngôn ngữ DartCascade Notation (..)

Cascade Notation (..) trong Dart

🎯 Mục tiêu: Chain operations trên cùng object.


💡 Cú pháp

var button = Button() ..text = "Click me" ..color = Colors.blue ..onClick = handleClick; // Tương đương var button = Button(); button.text = "Click me"; button.color = Colors.blue; button.onClick = handleClick;

📝 Với method calls

var list = <int>[] ..add(1) ..add(2) ..add(3) ..shuffle();

🔧 Null-aware cascade (?..)`

Button? button; button ?..text = "Click" ..color = Colors.red;

📱 Trong Flutter

final controller = TextEditingController() ..text = "Initial" ..selection = TextSelection.collapsed(offset: 0) ..addListener(() => print("Changed"));

✅ Checklist

  • .. returns the object, not the result
  • Useful for builders/initialization
  • ?.. for nullable objects

Last updated on