Spring for Dart

Spring for Dart brings the programming model of Spring Framework and Spring Boot, the Java frameworks, to Dart and Flutter. You write plain classes and mark them with annotations such as @Service() or @GetMapping('/books'). The framework creates the objects, connects them, configures them from application.properties or application.yml, and runs the application.

Dart has no runtime reflection on Flutter, on the web or in AOT executables. So the annotations are read at build time: spring_core_dart_generator, a build_runner builder, writes plain Dart code that creates your objects, routes your requests and applies your aspects. The same application runs with dart run, as a compiled executable, in a Flutter app and in the browser.

What it is good at

Who it is for

You do not need to know Java or Spring. These pages explain every idea from the start. If you do know Spring, most annotations and property names are the ones you know; each package page lists the differences.

Get started in 5 minutes

A web server with one service and one endpoint.

1. Create a package and set its dependencies in pubspec.yaml. The web starter brings everything else it needs (the core, Spring Boot, logging); the generator and build_runner are only used at build time.

# pubspec.yaml
name: hello
environment:
  sdk: ^3.13.0

dependencies:
  spring_boot_starter_web: ^0.9.0

dev_dependencies:
  build_runner: ^2.17.0
  spring_core_dart_generator: ^0.9.0

2. Write the application in bin/main.dart. Everything in lib/ and in the directory of main is scanned; there is no list of classes to keep up to date.

// bin/main.dart
import 'package:spring_boot_starter_web/spring_boot_starter_web.dart';

import 'spring_gen/main.spring.dart'; // written by build_runner

// A bean: the container creates one Greeter and passes it where needed.
// The name comes from the configuration, "World" when it is not set.
@Service()
class Greeter {
  Greeter(@Value(r'${greeting.name:World}') this.name);

  final String name;

  String greet() => 'Hello, $name!';
}

// GET /hello answers with the greeting.
@RestController()
class HelloController {
  HelloController(this.greeter); // injected by type

  final Greeter greeter;

  @GetMapping('/hello')
  String hello() => greeter.greet();
}

@SpringBootApplication()
void main(List<String> args) => runApplication(args);

r'${greeting.name:World}' is a raw string: ${...} is Spring's placeholder, not Dart's interpolation.

3. Add a setting in lib/resources/application.properties:

greeting.name=Dart

4. Generate the code and run it:

dart pub get
dart run build_runner build      # writes bin/spring_gen/main.spring.dart
dart run bin/main.dart
curl localhost:8080/hello
# Hello, Dart!

Settings can be overridden without touching the file: dart run bin/main.dart --greeting.name=Ada, or GREETING_NAME=Ada dart run bin/main.dart.

Run build_runner again after you add or change annotations, or keep dart run build_runner watch running.

These pages

Page What it explains
Inversion of control and dependency injection The core idea: the container creates and wires your objects. Beans, injection, configuration, lifecycle, scopes and tests.
Comparisons Small side-by-side examples: get_it and dependency injection, the Observer pattern, logic around a method, HTTP clients.
The starters A tour of every package and what adding it gives you.
One context for all starters How starters are found, how they use each other's beans, and how your own beans replace theirs.
Your own starter Writing a package that adds beans to every application that depends on it.

The packages

Package What it is Documentation
spring_core_dart The container: beans, configuration, profiles, events, validation, JSON, AOP spring_core_dart
spring_core_dart_generator The build_runner builder that reads the annotations and writes the code spring_core_dart_generator
spring_boot_dart Starts an application from @SpringBootApplication, reads the configuration, adds the starters spring_boot_dart
spring_boot_starter_web An HTTP server with @RestController classes spring_boot_starter_web
Other packages WebSocket, data, cache, scheduling, Feign, security, OAuth 2.0 client, actuator, logging, test, Flutter, navigation, Flutter security, search, and the libraries they build on The starters

An application lists the starters it uses, and nothing they bring: a web server lists spring_boot_starter_web alone, and gets spring_core_dart and spring_boot_dart through it.

In Spring (Java)

The ideas are the same as in Spring: beans, the application context, auto-configuration, starters, profiles, @ConfigurationProperties. What differs comes from Dart: