Preparing packages and code generation
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
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.
macos/Podfile to platform :osx, '12.0'post_installmacos/Runner.xcodeproj/project.pbxproj, replace every MACOSX_DEPLOYMENT_TARGET = 10.15; with 12.0;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.
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: truelib/ that have annotations (such as @riverpod) and a part directive. You don’t need to specify foldersgenerate_for), files placed elsewhere generate nothing, and the cause is hard to spotgenerate_for at that pointlib/main.dart
ProviderScope makes Riverpod available throughouttest/widget_test.dart (we rewrite tests in Chapter 11)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')),
),
);
}
}If generation misbehaves
Run dart run build_runner clean, then build again.
CHECK 0 / 3
Motion Note Hands-on