Dart Challenges for Every Skill Level
From mobile apps with Flutter to backend systems, Dart is a versatile and powerful language that’s quickly becoming a favorite among developers. For those new to the field, a solid grasp of Dart’s fundamentals is the key to building beautiful, high-performance applications. For seasoned developers, it’s about mastering asynchronous programming and advanced language features to create truly scalable solutions.
That’s why we’ve put together this collection of 30 hands-on Dart challenges. We’ve broken them down into three levels—10 for Junior, 10 for Mid-Level, and 10 for Senior developers—to help you build your skills, prepare for your next interview, or find the perfect candidate for your team.
Jump to Your Level
| 🎯 Junior Developer | 🚀 Mid-Level Developer | ⭐ Senior Developer |
Junior Developer Challenges
1. Hello, World!
The traditional first program to verify a working setup.
void main() {
print('Hello, World!');
}2. Sum of a List of Integers
A basic test of loops and list manipulation.
int sumOfList(List numbers) {
int sum = 0;
for (int number in numbers) {
sum += number;
}
return sum;
} 3. Reverse a String
A simple test of string manipulation.
String reverseString(String text) {
return text.split('').reversed.join('');
}4. Find the Largest Number in a List
A fundamental algorithm using a simple loop.
int findLargest(List numbers) {
int largest = numbers[0];
for (int number in numbers) {
if (number > largest) {
largest = number;
}
}
return largest;
} 5. Check if a String is a Palindrome
Tests string manipulation and comparison logic.
bool isPalindrome(String text) {
String reversedText = text.split('').reversed.join('');
return text == reversedText;
}6. FizzBuzz
The classic test of basic conditional logic and loops.
void fizzBuzz(int n) {
for (int i = 1; i 7. Simple Class for a Person
Introduces custom types and data organization.
class Person {
String name;
int age;
Person(this.name, this.age);
}8. Function that Returns a Map
A simple function to demonstrate map creation.
Map createPerson(String name, int age) {
return {
'name': name,
'age': age,
};
} 9. Simple Loop to Print Numbers 1-10
A basic loop to demonstrate syntax.
void printNumbers() {
for (int i = 1; i 10. Check if a Number is Positive, Negative, or Zero
Tests basic conditional logic.
String checkNumber(int number) {
if (number > 0) {
return 'Positive';
} else if (number Mid-Level Developer Challenges
1. Asynchronous Function with `Future` and `async/await`
A simple asynchronous function to demonstrate `Future`, `async`, and `await`.
Future fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'Data fetched';
} 2. Word Count in a String using a `Map`
Use a map to count the frequency of words in a text.
Map wordCount(String text) {
Map counts = {};
List words = text.split(' ');
for (String word in words) {
counts[word] = (counts[word] ?? 0) + 1;
}
return counts;
} 3. Implement a Stack using a `List`
A simple implementation of a stack using a list.
class Stack {
List _items = [];
void push(T item) => _items.add(item);
T pop() => _items.removeLast();
} 4. JSON Serialization and Deserialization
Convert Dart objects to and from JSON.
import 'dart:convert';
class User {
String name;
int age;
User(this.name, this.age);
Map toJson() => {'name': name, 'age': age};
factory User.fromJson(Map json) => User(json['name'], json['age']);
} 5. Find the First Non-Repeating Character in a String
A common interview question that can be solved with a map.
String firstNonRepeatingCharacter(String text) {
Map counts = {};
for (int i = 0; i 6. Class Inheritance with a `Shape` Base Class
A simple example of class inheritance.
abstract class Shape {
double get area;
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double get area => 3.14 * radius * radius;
}7. Higher-Order Function that Takes a Function as an Argument
A function that takes another function as an argument.
List transform(List numbers, int Function(int) transformer) {
return numbers.map(transformer).toList();
} 8. Error Handling with `try-catch`
A simple example of error handling with `try-catch`.
void main() {
try {
throw Exception('An error occurred');
} catch (e) {
print(e);
}
}9. Use of the Cascade Operator (`..`)
A simple example of the cascade operator.
class Person {
String name;
int age;
}
void main() {
Person person = Person()
..name = 'John Doe'
..age = 30;
}10. Implement a Simple Stream
A simple implementation of a stream.
Stream countStream(int max) async* {
for (int i = 1; i Senior Developer Challenges
1. Implement a Generic Repository Pattern
A generic repository pattern for data access.
abstract class Repository {
Future getById(int id);
Future> getAll();
Future save(T entity);
Future delete(T entity);
}
2. Create a Custom BLoC (Business Logic Component)
A simple BLoC for managing state.
import 'dart:async';
abstract class CounterEvent {}
class IncrementEvent extends CounterEvent {}
class CounterBloc {
int _counter = 0;
final _counterStateController = StreamController();
StreamSink get _inCounter => _counterStateController.sink;
Stream get counter => _counterStateController.stream;
final _counterEventController = StreamController();
Sink get counterEventSink => _counterEventController.sink;
CounterBloc() {
_counterEventController.stream.listen(_mapEventToState);
}
void _mapEventToState(CounterEvent event) {
if (event is IncrementEvent) {
_counter++;
}
_inCounter.add(_counter);
}
void dispose() {
_counterStateController.close();
_counterEventController.close();
}
} 3. Use Isolates for Parallel Processing
A simple example of using isolates for parallel processing.
import 'dart:isolate';
void main() async {
final receivePort = ReceivePort();
await Isolate.spawn(heavyComputation, receivePort.sendPort);
receivePort.listen((message) {
print(message);
});
}
void heavyComputation(SendPort sendPort) {
int sum = 0;
for (int i = 0; i 4. Create a Custom Extension Method
A custom extension method for a string.
extension StringExtensions on String {
String capitalize() {
return this[0].toUpperCase() + this.substring(1);
}
}5. Implement a Debounce Function
A debounce function to limit the rate of execution.
import 'dart:async';
Function debounce(Function fn, Duration duration) {
Timer _timer;
return () {
_timer?.cancel();
_timer = Timer(duration, fn);
};
}6. Advanced Error Handling with a Custom `Exception` Class
A custom exception class for advanced error handling.
class CustomException implements Exception {
final String message;
CustomException(this.message);
}
void main() {
try {
throw CustomException('An error occurred');
} on CustomException catch (e) {
print(e.message);
}
}7. Use of Mixins to Add Functionality to a Class
A simple example of using mixins to add functionality to a class.
mixin Flyable {
void fly() => print('Flying');
}
class Bird with Flyable {}8. Create a Complex Data Model with Immutability
A complex data model with immutability.
class User {
final String name;
final int age;
User(this.name, this.age);
User copyWith({String name, int age}) {
return User(
name ?? this.name,
age ?? this.age,
);
}
}9. Implement a Singleton Pattern
A simple implementation of a singleton pattern.
class Singleton {
static final Singleton _instance = Singleton._internal();
factory Singleton() => _instance;
Singleton._internal();
}10. Use of `compute` for Heavy Computations in Flutter
A simple example of using `compute` for heavy computations in Flutter.
import 'package:flutter/foundation.dart';
Future heavyComputation(int value) async {
int sum = 0;
for (int i = 0; i Tips to Prepare for Dart Coding Challenges
- Master Asynchronous Programming: Dart's single-threaded nature makes `Future`, `async`, and `await` essential. Be prepared to handle asynchronous operations gracefully.
- Understand Dart's Type System: Dart is a strongly typed language. Understanding its type system will help you write more robust and maintainable code.
- Know the Flutter Framework: If you're interviewing for a Flutter position, be prepared to answer questions about the framework's architecture and best practices.
- Practice with Collections: Dart's collection framework is powerful. Practice with `List`, `Map`, and `Set` to solve common problems.
- Learn about State Management: If you're working with Flutter, be prepared to discuss different state management solutions like BLoC, Provider, and Riverpod.
Conclusion
Dart's modern features and strong ties to the Flutter framework make it an exciting language to learn and master. By practicing with these challenges, you'll be well-prepared to tackle any interview and build amazing applications. Happy coding!