1. Project Setup

Preparing packages and code generation

Goals of this chapter

1

Create the project

For macOS / Windows / Android / iOS

2

Add packages

Riverpod, freezed, go_router, drift

3

Run code generation

build.yaml and build_runner

Create the project

flutter create --org com.example --platforms=macos,windows,android,ios motion_app
cd motion_app

If the macOS build fails

With recent Xcode, a freshly created project fails to build because the supported macOS version (10.15) is too old. Raise it to 12.0 with the steps on the next slide.

Set the supported macOS version to 12.0

  1. Change the first line of macos/Podfile to platform :osx, '12.0'
  2. In the same file, add the 3 lines below to post_install
  3. In macos/Runner.xcodeproj/project.pbxproj, replace every MACOSX_DEPLOYMENT_TARGET = 10.15; with 12.0;
macos/Podfile
post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_macos_build_settings(target)
    target.build_configurations.each do |config|
      config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '12.0'
    end
  end
end

Add packages

flutter pub add flutter_localizations --sdk=flutter
flutter pub add 'flutter_riverpod:^3.0.0' 'hooks_riverpod:^3.0.0' \
  flutter_hooks riverpod_annotation freezed_annotation go_router \
  drift drift_flutter path_provider uuid intl \
  dev:build_runner dev:riverpod_generator dev:freezed dev:drift_dev

Pitfalls when adding packages

Pin Riverpod to 3.x

If you add it without a version, the old Riverpod 2.x may be selected and conflict with the code-generation packages.

Add flutter_localizations first

If intl is added first, a version newer than the one pinned by the Flutter SDK is chosen, and in Chapter 10 the localization packages can no longer be installed.

  • BLE (universal_ble) is added in Chapter 8, shared_preferences in Chapter 9, and localization (slang) in Chapter 10

Create build.yaml

Create it in the project root. It only specifies how drift stores date-times.

build.yaml
# Code generation (build_runner) settings
# Don't restrict target folders; scan everything under lib/. Works unchanged if the folder layout changes or in another app
targets:
  $default:
    builders:
      drift_dev:
        options:
          # Store DateTime as a string (ISO 8601). Keeps the time zone and is readable when opening the DB directly
          store_date_time_values_as_text: true

Key points of build.yaml

  • The riverpod / freezed / drift generators only generate for files under lib/ that have annotations (such as @riverpod) and a part directive. You don’t need to specify folders
  • If you specify folders (generate_for), files placed elsewhere generate nothing, and the cause is hard to spot
  • If the app grows and generation gets slow, narrow the targets with generate_for at that point

Write the entry point (main)

lib/main.dart
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:motion_app/app.dart';

void main() {
  runApp(const ProviderScope(child: App()));
}
  • Wrapping the app in ProviderScope makes Riverpod available throughout
  • Delete the existing test/widget_test.dart (we rewrite tests in Chapter 11)

Write the entry point (App)

lib/app.dart
import 'package:flutter/material.dart';

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Motion Note',
      theme: ThemeData(colorSchemeSeed: Colors.teal),
      home: const Scaffold(
        body: Center(child: Text('Motion Note')),
      ),
    );
  }
}

Run code generation

dart run build_runner build   # generate once
dart run build_runner watch   # regenerate on every save (use this while developing)
flutter run -d macos

If generation misbehaves

Run dart run build_runner clean, then build again.

Checkpoint

CHECK 0 / 3