Skip to content

polyglot-learn/LearnDart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

416 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LearnDart

A hands-on collection of small, runnable Dart programs — from your first Hello, world! to classic algorithms — written for two audiences at once.

Dart SDK License: MIT PRs Welcome Style: Dart

Inspired by TheAlgorithms/Python. Every file in this repository is a self-contained lesson: you can open it, read it top-to-bottom, and run it with a single command.


Table of contents


Why LearnDart?

Most language tutorials fall into one of two traps: they either bury you in features before you can write a program, or they hand you a giant final application with no explanation of the pieces. LearnDart takes a third path — one concept per file, one file per commit. You can read the code, the comments, and (if you want) the git history and see exactly how the language was introduced, in order.

  • Read it as a book — walk through basics/ in order, no setup beyond installing Dart.
  • Read it as a reference — jump straight to sorts/, graphs/, dynamic_programming/, etc. when you need a Dart implementation of a classic algorithm.
  • Read it as a git log — every commit is atomic and titled Add: <path> — <what it teaches>.

Who this is for

Audience Where to start What you'll get
Non-programmers learning to code for the first time basics/00_hello_world/ Files are numbered 00, 01, 02... so you always know the next step. Each basics/ file has plain-English comments that explain what the code does and why.
Programmers coming from Python, JavaScript, Go, Java, C# or similar basics/08_null_safety/, then the algorithm categories Skim the basics/ folders for Dart-specific idioms (sound null safety, records, patterns, async/await, mixins), then dive into idiomatic Dart implementations of algorithms you already know.
CS students looking for clean reference implementations Any algorithm category below Every algorithm is a small, focused file with a main() that exercises it. No frameworks, no packages — just Dart and the SDK.

Quick start

  1. Install Dart — see dart.dev/get-dart. Any Dart SDK 3.4.0 or newer works.

  2. Clone the repo:

    git clone https://github.com/polyglot-learn/LearnDart.git
    cd LearnDart
  3. Run any file:

    dart run basics/00_hello_world/hello_world.dart

That's the whole workflow. There is no build step, no pub get, no packages to install — the repo has zero third-party dependencies.

The learning path (basics/)

Follow the numbered folders in order. Inside each folder, read the files in alphabetical order.

# Folder You'll learn Files
00 00_hello_world/ main(), print, running a Dart file 1
01 01_variables/ var, final vs const, late initialization 3
02 02_types/ int, double, String, bool, Object, dynamic, inference 5
03 03_operators/ Arithmetic, comparison, logical, null-aware operators 4
04 04_strings/ Interpolation, multiline / raw strings, common methods 3
05 05_control_flow/ if/else, switch statement & expression, for, while, do-while, break/continue 7
06 06_functions/ Positional & named parameters, arrow syntax, higher-order functions, closures 6
07 07_collections/ List, Set, Map, iteration, spread operator, collection-if/for 7
08 08_null_safety/ Nullable types, flow-based narrowing, the ! operator 3
09 09_classes/ Fields, constructors, getters/setters, inheritance, abstract classes, mixins, enums 8
10 10_async/ Future, async/await, Stream and async* generators 3
11 11_errors/ try/catch/finally, throwing exceptions, custom exception types 3

Algorithm categories

Once you're comfortable with the language, these folders contain clean, standalone implementations of the classics. Each file is a tiny module: a couple of top-level functions and a main() that demonstrates them.

sorts/ — sorting algorithms

File Idea Time
bubble_sort.dart Repeated adjacent-pair swap with early-exit O(n²)
selection_sort.dart Repeatedly pick the minimum from the unsorted tail O(n²)
insertion_sort.dart Grow a sorted prefix one element at a time O(n²)
merge_sort.dart Divide-and-conquer with a linear-time merge O(n log n)
quick_sort.dart Lomuto partition, in-place recursion O(n log n) avg
heap_sort.dart Max-heapify, then repeatedly extract the maximum O(n log n)
counting_sort.dart Non-comparison sort for bounded integer ranges O(n + k)
shell_sort.dart Gapped insertion sort with halving gap sequence O(n²) worst

searches/ — search algorithms

File Idea Precondition
linear_search.dart Sequential scan for the target
binary_search.dart Iterative O(log n) half-interval search sorted
binary_search_recursive.dart Recursive variant of binary search sorted
ternary_search.dart Split the range into thirds each iteration sorted
jump_search.dart √n block jumps followed by a linear scan sorted

data_structures/ — the classics

File What it is
stack.dart Generic LIFO with push, pop, peek
queue.dart Generic FIFO backed by dart:collection Queue
linked_list.dart Singly linked list
doubly_linked_list.dart Doubly linked list with bidirectional traversal
binary_tree.dart Pre-, in- and post-order traversals
binary_search_tree.dart BST with insert, contains, in-order enumeration
hash_table.dart Open hash table with per-bucket chaining
min_heap.dart Array-backed min-heap with sift-up / sift-down
graph_adjacency_list.dart Undirected graph via Map<T, Set<T>>

maths/ — number theory & arithmetic

File Topic
gcd.dart / gcd_recursive.dart Iterative and recursive Euclidean algorithm
lcm.dart Least common multiple via gcd
factorial_iterative.dart / factorial_recursive.dart Factorial using BigInt
fibonacci_iterative.dart / fibonacci_recursive.dart / fibonacci_memoized.dart Three ways to compute Fibonacci — with cost comparisons in the comments
is_prime.dart Trial division up to √n
sieve_of_eratosthenes.dart Generate primes up to a limit
power_iterative.dart / power_recursive.dart Exponentiation by squaring

strings/ — string algorithms

ciphers/ — classical ciphers

dynamic_programming/ — DP classics

graphs/ — graph algorithms

  • bfs.dart — breadth-first traversal with a queue and visited set
  • dfs.dart — recursive depth-first traversal
  • dijkstra.dart — shortest paths for non-negative edge weights

recursion/ — classic recursion

See DIRECTORY.md for a flat alphabetical index of every file.

A taste of the code

Your first Dart programbasics/00_hello_world/hello_world.dart:

// Every Dart program starts running from a function called `main`.
void main() {
  print('Hello, world!');
}
$ dart run basics/00_hello_world/hello_world.dart
Hello, world!

Dart 3 switch expression with patternsbasics/05_control_flow/switch_expression.dart:

String describe(int n) => switch (n) {
      0 => 'zero',
      1 || 2 || 3 => 'small',
      < 0 => 'negative',
      _ => 'large or unusual',
    };

A generic stackdata_structures/stack.dart:

class Stack<T> {
  final List<T> _items = [];
  void push(T v) => _items.add(v);
  T pop() => _items.removeLast();
  T peek() => _items.last;
  bool get isEmpty => _items.isEmpty;
}

Fibonacci — three ways, side by side in maths/:

// fibonacci_iterative.dart  — O(n) time, O(1) space
int fib(int n) {
  int a = 0, b = 1;
  for (int i = 2; i <= n; i++) { final c = a + b; a = b; b = c; }
  return b;
}

// fibonacci_recursive.dart  — O(2^n), for pedagogical contrast
int fib(int n) => n < 2 ? n : fib(n - 1) + fib(n - 2);

// fibonacci_memoized.dart   — recursion + cache, back to O(n)
final _cache = {0: BigInt.zero, 1: BigInt.one};
BigInt fib(int n) => _cache[n] ??= fib(n - 1) + fib(n - 2);

How this repo is organized

LearnDart/
├── basics/                 numbered teaching folders 00_ ... 11_
│   ├── 00_hello_world/
│   ├── 01_variables/
│   ├── 02_types/
│   └── ...
├── sorts/                  one algorithm per file
├── searches/
├── data_structures/
├── maths/
├── strings/
├── ciphers/
├── dynamic_programming/
├── graphs/
├── recursion/
├── DIRECTORY.md            flat index of every .dart file
├── CONTRIBUTING.md         file and commit conventions
├── LICENSE                 MIT
├── README.md               you are here
└── pubspec.yaml            Dart SDK constraint, no dependencies

Repository conventions

  • One concept per file. Splittable? Split it.
  • One file per commit. The git history is the reading order.
  • Every file runs. No file exists without a main() you can execute directly.
  • Zero dependencies. Every sample runs against the pure Dart SDK.
  • basics/ files teach. They have prose comments explaining the what and the why.
  • Algorithm files stay clean. They only comment when the why is non-obvious.
  • Iterative + recursive = two commits. Same algorithm, two shapes — they get their own files.

Full details in CONTRIBUTING.md.

Roadmap

More samples land in later sessions. Planned expansions:

  • More sorts — radix, bucket, timsort, tree sort
  • More searches — interpolation, exponential, Fibonacci search
  • More data structures — trie, AVL/red-black trees, disjoint set union, segment tree, Fenwick tree, LRU cache
  • More graph algorithms — Bellman-Ford, Floyd-Warshall, Prim, Kruskal, topological sort, Union-Find
  • More DP — edit distance, matrix chain multiplication, longest increasing subsequence, rod cutting
  • New categoriesbit_manipulation/, backtracking/, greedy/, number_theory/, geometry/
  • Classic problems — N-queens, sudoku, maze solvers, sliding-window puzzles
  • Advanced Dart — isolates, dart:io file & socket samples, FFI

Contributing

New samples are always welcome. Please read CONTRIBUTING.md first — the short version:

  1. One concept per file. Splittable? Split it.
  2. One file per commit. Commit message format: Add: <path> — <one-line what it teaches>.
  3. dart format . before you submit.
  4. dart analyze is clean.
  5. Update DIRECTORY.md when you add files.

Inspiration

License

Released under the MIT License.

About

A collection of runnable Dart samples — from Hello World to classic algorithms — for both beginners and programmers coming from another language. Inspired by TheAlgorithms/Python.

Resources

License

Contributing

Stars

7 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages