Learn Dart - 10 Code Examples & CST Typing Practice Test
Dart is a modern, object-oriented programming language developed by Google, optimized for building web, server, and mobile applications, particularly for Flutter. It emphasizes performance, strong typing, and productive development.
View all 10 Dart code examples →
Learn DART with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
Dart Theme Toggle and Counter
void main() {
int count = 0;
bool isDark = false;
void updateUI() {
print('Counter: $count');
print('Theme: ${isDark ? 'Dark' : 'Light'}');
}
void increment() { count++; updateUI(); }
void decrement() { count--; updateUI(); }
void reset() { count = 0; updateUI(); }
void toggleTheme() { isDark = !isDark; updateUI(); }
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
}
Demonstrates a simple counter with theme toggling in Dart, runnable in console or Flutter environment.
Dart Counter with History
void main() {
int count = 0;
List<String> history = [];
void updateUI() {
print('Counter: $count');
print('History: ${history.join(', ')}');
}
void increment() {
count++;
history.add('Incremented to $count');
updateUI();
}
void decrement() {
count--;
history.add('Decremented to $count');
updateUI();
}
void reset() {
count = 0;
history.clear();
history.add('Counter reset');
updateUI();
}
updateUI();
increment();
increment();
decrement();
reset();
}
A console-based counter that logs each increment/decrement action in a history list.
Dart Counter with Conditional Theme
void main() {
int count = 0;
bool isDark = false;
void updateUI() {
isDark = count % 2 == 0;
print('Counter: $count');
print('Theme: ${isDark ? 'Dark' : 'Light'}');
}
void increment() { count++; updateUI(); }
void decrement() { count--; updateUI(); }
void reset() { count = 0; updateUI(); }
updateUI();
increment();
increment();
increment();
decrement();
reset();
}
Counter that switches theme based on value thresholds in Dart console app.
Dart Counter with Lambda Actions
void main() {
int count = 0;
bool isDark = false;
var updateUI = () => print('Counter: $count, Theme: ${isDark ? 'Dark' : 'Light'}');
var increment = () { count++; updateUI(); };
var decrement = () { count--; updateUI(); };
var reset = () { count = 0; updateUI(); };
var toggleTheme = () { isDark = !isDark; updateUI(); };
updateUI();
increment();
toggleTheme();
decrement();
reset();
}
Demonstrates using Dart anonymous functions (lambdas) for counter actions and theme toggling.
Dart Counter with Max Limit
void main() {
int count = 0;
int maxLimit = 5;
void updateUI() => print('Counter: $count');
void increment() {
if(count < maxLimit) count++;
updateUI();
}
void decrement() { count--; updateUI(); }
void reset() { count = 0; updateUI(); }
updateUI();
increment();
increment();
increment();
increment();
increment();
increment(); // won't exceed maxLimit
reset();
}
A counter that stops incrementing after reaching a maximum limit.
Dart Counter with Timer Auto-Increment
import 'dart:async';
void main() {
int count = 0;
Timer.periodic(Duration(seconds: 1), (timer) {
count++;
print('Counter: $count');
if(count >= 5) timer.cancel();
});
}
Automatically increments the counter every second using Dart Timer.
Dart Counter with User Input
import 'dart:io';
void main() {
int count = 0;
while(true) {
print('Counter: $count');
stdout.write('Enter i/d/r/q: ');
String? input = stdin.readLineSync();
if(input == 'i') count++;
else if(input == 'd') count--;
else if(input == 'r') count = 0;
else if(input == 'q') break;
}
}
A console-based counter that takes user input to increment, decrement, or reset.
Dart Counter with Event Callbacks
void main() {
int count = 0;
void onUpdate(int value) => print('Counter: $value');
void increment(void Function(int) callback) {
count++;
callback(count);
}
void decrement(void Function(int) callback) {
count--;
callback(count);
}
void reset(void Function(int) callback) {
count = 0;
callback(count);
}
increment(onUpdate);
increment(onUpdate);
decrement(onUpdate);
reset(onUpdate);
}
Uses callback functions to handle increment, decrement, and reset events.
Dart Counter with Custom Step
void main() {
int count = 0;
int step = 3;
void updateUI() => print('Counter: $count');
void increment() { count += step; updateUI(); }
void decrement() { count -= step; updateUI(); }
void reset() { count = 0; updateUI(); }
updateUI();
increment();
decrement();
reset();
}
A counter that can increment or decrement by a custom step value.
Dart Counter with Persistent Storage
import 'dart:io';
void main() {
File file = File('counter.txt');
int count = 0;
if(file.existsSync()) {
count = int.parse(file.readAsStringSync());
}
void updateUI() {
print('Counter: $count');
file.writeAsStringSync(count.toString());
}
void increment() { count++; updateUI(); }
void decrement() { count--; updateUI(); }
void reset() { count = 0; updateUI(); }
updateUI();
increment();
decrement();
reset();
}
Stores counter value in a file to persist between program runs (console app).
Frequently Asked Questions about Dart
What is Dart?
Dart is a modern, object-oriented programming language developed by Google, optimized for building web, server, and mobile applications, particularly for Flutter. It emphasizes performance, strong typing, and productive development.
What are the primary use cases for Dart?
Mobile app development with Flutter. Web applications and progressive web apps (PWAs). Server-side applications with Dart VM. Command-line tools and scripts. Cross-platform desktop applications
What are the strengths of Dart?
Fast compilation and runtime performance. Seamless cross-platform development with Flutter. Strong tooling support and IDE integration. Modern syntax and developer-friendly features. Large ecosystem and package management via pub.dev
What are the limitations of Dart?
Relatively smaller ecosystem compared to JavaScript or Python. Limited support for low-level system programming. Not as widely adopted outside Flutter ecosystem. Web performance depends on JavaScript compilation. Smaller talent pool compared to more established languages
How can I practice Dart typing speed?
CodeSpeedTest offers 10+ real Dart code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.