String Interpolation trong Dart
1. Basic Interpolation
var name = 'Alice';
var age = 25;
print('Hello, $name!');
print('I am $age years old');2. Expressions
var a = 10;
var b = 20;
print('Sum: ${a + b}');
print('Max: ${a > b ? a : b}');3. Properties và Methods
var numbers = [1, 2, 3, 4, 5];
print('Count: ${numbers.length}');
print('Sum: ${numbers.reduce((a, b) => a + b)}');4. Multi-line với Interpolation
var name = 'Bob';
var items = ['Apple', 'Banana'];
var message = '''
Hello, $name!
You have ${items.length} items:
${items.join(', ')}
''';📝 Tóm tắt
$variable- Simple interpolation$\{expression\}- Expression interpolation- Hoạt động trong regular và multi-line strings
Last updated on