FlutterAdvanced 2 min read

Flutter app architecture: UI, state and repository boundaries

Separate interface rendering from data access with a small repository contract that can be replaced in tests.

What you will learn

  • Assign a clear owner to UI, state and data access.
  • Define a replaceable repository contract.
  • Test a boundary without a network request.
On this page

Architecture is a set of responsibility boundaries, not a required number of directories. A small feature can start with a view, a state owner and a repository. Add more layers only when they make a real dependency or rule clearer.

Follow the direction of data

The view sends an intent to the state owner. The state owner calls a repository, then exposes a renderable state. The repository hides how information is obtained. Raw HTTP responses and SQL rows should not spread through widget build methods.

Make the contract replaceable

This Dart-only example uses an in-memory repository so the boundary is visible without a network service.

Worked example

This is a complete example. Open it with Run example, then use Run inside DartPad.

main.dart
abstract interface class LessonRepository {
  Future<List<String>> loadTitles();
}

class MemoryLessons implements LessonRepository {
  @override
  Future<List<String>> loadTitles() async => ['Constraints', 'State ownership'];
}

Future<String> summarizeLessons(LessonRepository repository) async {
  final titles = await repository.loadTitles();
  return '${titles.length} lessons: ${titles.join(', ')}';
}

Future<void> main() async {
  print(await summarizeLessons(MemoryLessons()));
}

Read the result

The summary operation depends on a contract instead of a concrete network client. A test can supply an empty or failing repository. This does not require a dependency-injection package; constructor or parameter injection can establish the boundary.

Replace the dependency without changing the operation

summarizeLessons needs lesson titles, not a particular HTTP library. MemoryLessons satisfies that contract in the example. A test can provide an empty or failing implementation while calling exactly the same operation.

This is what the boundary buys you: the difficult behavior can be exercised without a live server or a complete screen. It does not require a directory for every class. Start with clear responsibilities, then separate files as navigating them becomes useful.

Continue with JSON validation at the external-data boundary and MVVM state when the view needs explicit loading and failure representations.

Practice and verification

Implement a failing repository and decide which layer converts that failure into a UI state. Then draw imports between view, state owner and data source. Remove a dependency that points in the wrong direction.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.