Dart Output
Display text and data to users in Dart programs
📺 Dart Output Methods
Learn how to display information to users in Dart programs. The print() function is the most common way to show output, making it essential for debugging and user interaction.
// Basic output in Dart
void main() {
print('Hello, World!');
}
Output:
Hello, World!
Output Functions
print()
Display text with new line
print('Hello World');
stdout.write()
Display text without new line
stdout.write('Hello ');
Variables
Display variable values
var name = 'John';
print(name);
String Interpolation
Embed variables in strings
print('Hello $name');
🔹 Basic Print Function
The print() function is the simplest way to display output:
void main() {
// Print simple text
print('Welcome to Dart!');
// Print numbers
print(42);
print(3.14);
// Print boolean values
print(true);
print(false);
}
Output:
Welcome to Dart!
42
3.14
true
false
🔹 Printing Variables
Display the values stored in variables:
void main() {
// Create variables
var name = 'Alice';
var age = 30;
var height = 5.6;
var isStudent = false;
// Print variables
print(name);
print(age);
print(height);
print(isStudent);
}
Output:
Alice
30
5.6
false
🔹 String Interpolation
Embed variables directly inside strings using $ symbol:
🔸 Basic Interpolation
void main() {
var name = 'Bob';
var age = 25;
// Using $ for simple variables
print('My name is $name');
print('I am $age years old');
// Combining multiple variables
print('Hello $name, you are $age years old');
}
Output:
My name is Bob
I am 25 years old
Hello Bob, you are 25 years old
🔸 Expression Interpolation
void main() {
var price = 100;
var quantity = 3;
// Using ${} for expressions
print('Total cost: \$${price * quantity}');
print('Next year I will be ${age + 1} years old');
print('Name length: ${name.length} characters');
}
Output:
Total cost: $300
Next year I will be 26 years old
Name length: 3 characters
🔹 String Concatenation
Join strings together using the + operator:
void main() {
var firstName = 'John';
var lastName = 'Doe';
// Using + operator
print('Full name: ' + firstName + ' ' + lastName);
// Mixing strings and numbers
var score = 95;
print('Your score is: ' + score.toString());
// Multiple concatenations
print('Hello ' + firstName + '! Welcome to Dart programming.');
}
Output:
Full name: John Doe
Your score is: 95
Hello John! Welcome to Dart programming.
🔹 Advanced Output Techniques
More sophisticated ways to display output:
🔸 Multiline Strings
void main() {
var poem = '''
Roses are red,
Violets are blue,
Dart is awesome,
And so are you!
''';
print(poem);
}
🔸 Formatted Output
import 'dart:io';
void main() {
var name = 'Alice';
var score = 87.5;
// Using stdout.write (no automatic newline)
stdout.write('Enter your name: ');
stdout.write(name);
stdout.write('\n');
// Formatted numbers
print('Score: ${score.toStringAsFixed(1)}%');
}
Output:
Roses are red,
Violets are blue,
Dart is awesome,
And so are you!
Enter your name: Alice
Score: 87.5%