spring_boot_starter_flutter

spring_boot_starter_flutter brings Spring Boot into a Flutter app. You write your services and your app state as plain classes marked @Component(). The framework creates one object of each, a bean, and connects them: a bean that needs another one asks for it in its constructor. All beans live in the application context, which starts when the app starts. The starter puts that context into the widget tree, so any widget can get the beans it needs, and widgets that show a bean's state rebuild when it changes.

You do not need to know Spring to use it. If you do, you will find the same model: @Component, @Value, @ConfigurationProperties, profiles, events and application.yml.

Why

A Flutter app has no standard place for its services and its shared state. Each app picks a state library, passes objects through constructors, and keeps its settings in Dart code. The starter gives one model for all of it:

Setup

# pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  spring_boot_starter_flutter: ^0.9.0

dev_dependencies:
  build_runner: ^2.17.0
  spring_core_dart_generator: ^0.9.0   # required: it writes the application
  flutter_test:
    sdk: flutter
  spring_boot_starter_test: ^0.9.0     # springBootTest and BeanOverride, for tests

Then:

  1. Put @SpringBootApplication() on main in lib/main.dart.
  2. Run dart run build_runner watch (or build). The generator writes lib/spring_gen/main.spring.dart, with springApplication: the application, with every bean found under lib/. Keep watch running while you work.
  3. Put configuration files and message files into lib/resources/. The generator embeds them into the generated code, so they need no flutter: assets: entry.

The Flutter packages need Flutter 3.50 or later (Dart 3.14).

The starter has no design library: it imports package:flutter/widgets.dart only, and an app of the default type widgets has neither Material nor Cupertino in its build. An app with Material or Cupertino widgets adds spring_boot_starter_flutter_material or spring_boot_starter_flutter_cupertino, and material_ui or cupertino_ui. See Design libraries.

One import is enough. package:spring_boot_starter_flutter/spring_boot_starter_flutter.dart also exports spring_core_dart, spring_boot_dart, spring_web_dart (the HTTP clients) and spring_boot_starter_logging. Two names are left out because Flutter has its own: Boot's startup Banner (Flutter has a Banner widget) and the validation constraint Size (dart:ui has Size), which is exported as SizeConstraint.

Example

A complete counter app. It uses Material's Scaffold, FloatingActionButton and Icons, so it adds the Material starter and material_ui:

# pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  material_ui: ^1.11.0
  spring_boot_starter_flutter_material: ^0.9.0   # brings spring_boot_starter_flutter
// lib/main.dart
import 'package:material_ui/material_ui.dart';
import 'package:spring_boot_starter_flutter_material/spring_boot_starter_flutter_material.dart';

import 'spring_gen/main.spring.dart'; // written by the generator

@SpringBootApplication()
void main() => runApp(
  SpringApplicationWidget(
    application: () => springApplication,
    // The app widget around it is built from spring.flutter.app.*.
    home: const CounterPage(),
  ),
);

// The count is a bean, not a State: any widget or service can read and
// change it. The generated proxy notifies the listeners after every method.
@ChangeNotifierComponent()
class Counter {
  int value = 0;
  void increment() => value++;
}

// Lists the bean it needs in its type and gets it as `beans`.
class CounterPage extends SpringStatelessWidget<(Counter,)> {
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final (counter,) = beans;
    return Scaffold(
      body: const Center(child: CounterText()),
      // Only calls a method, so a tap does not rebuild the page.
      floatingActionButton: FloatingActionButton(
        onPressed: counter.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}

// Reads counter.value in build, so it rebuilds after every increment.
class CounterText extends SpringStatelessWidget<(Counter,)> {
  const CounterText({super.key});

  @override
  Widget build(BuildContext context) {
    final (counter,) = beans;
    return Text('${counter.value}');
  }
}
# lib/resources/application.yml
spring:
  flutter:
    app:
      # No type: Scaffold needs a MaterialApp, and spring_boot_starter_flutter_material,
      # the one design starter, makes the app one
      title: Counter
    theme:
      seed-color: deepPurple

Running it

dart run build_runner watch   # writes lib/spring_gen/main.spring.dart
flutter run

What happens at the start:

INFO 82749 --- [main] spring_boot_dart.SpringApplication : The following 3 profiles are active: "desktop", "macos", "debug"

What is inside

Getting started:

Beans and state:

Themes:

The start and tools:

Content and files:

Platforms:

Reference:

In Spring (Java)

Spring has no widget tree. The nearest thing to SpringApplicationWidget is the main method of a Spring Boot app:

@SpringBootApplication
class ShopApplication {
  public static void main(String[] args) {
    SpringApplication.run(ShopApplication.class, args);
  }
}

Differences that matter:

See also