flowchart LR V["View"] -- "ref.watch(state)" --> VM["ViewModel"] V -- "notifier.addUser()" --> VM VM -- "updates state" --> V
Building the View and ViewModel separately
1
Create the State
An immutable state class with freezed
2
Create the ViewModel
A Notifier with @riverpod
3
Create the View
A HookConsumerWidget that only displays
View (screen)
ref.watchViewModel
state = state.copyWith(...)flowchart LR V["View"] -- "ref.watch(state)" --> VM["ViewModel"] V -- "notifier.addUser()" --> VM VM -- "updates state" --> V
lib/ui/user_list/view_model/user_list_view_model.dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'user_list_view_model.g.dart';
part 'user_list_view_model.freezed.dart';
@freezed
abstract class UserListState with _$UserListState {
const factory UserListState({
@Default([]) List<String> names,
String? errorMessage,
}) = _UserListState;
}
@riverpod
class UserListViewModel extends _$UserListViewModel {
@override
UserListState build() => const UserListState();
// true if added
bool addUser(String name) {
final trimmed = name.trim();
if (trimmed.isEmpty) {
state = state.copyWith(errorMessage: '名前を入力してください');
return false;
}
state = state.copyWith(
names: [...state.names, trimmed],
errorMessage: null,
);
return true;
}
}@freezed abstract class → copyWith and == are generated automatically@Default(...) sets the initial value@riverpod → userListViewModelProvider is generated automaticallypart '...g.dart' / part '...freezed.dart' are generated files. It’s fine if they’re red at firstlib/ui/user_list/view/user_list_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:motion_app/ui/user_list/view_model/user_list_view_model.dart';
class UserListPage extends HookConsumerWidget {
const UserListPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(userListViewModelProvider);
final notifier = ref.read(userListViewModelProvider.notifier);
final nameController = useTextEditingController();
return Scaffold(
appBar: AppBar(title: const Text('ユーザー一覧')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: TextField(
controller: nameController,
decoration: InputDecoration(
labelText: '名前',
errorText: state.errorMessage,
),
),
),
const SizedBox(width: 16),
FilledButton(
onPressed: () {
if (notifier.addUser(nameController.text)) {
nameController.clear();
}
},
child: const Text('追加'),
),
],
),
),
Expanded(
child: ListView(
children: [
for (final name in state.names)
ListTile(leading: const Icon(Icons.person), title: Text(name)),
],
),
),
],
),
);
}
}| Code | Meaning |
|---|---|
ref.watch(provider) |
Reads the state. Redraws the screen when it changes |
ref.read(provider.notifier) |
Calls ViewModel methods |
useTextEditingController() |
Controller for the input field. Cleanup is automatic too |
Without watch (StatefulWidget + setState)
ref.read: it isn’t redrawn when the value changes → always use ref.watch for values you displaySet home in app.dart to const UserListPage().
lib/app.dart
import 'package:flutter/material.dart';
import 'package:motion_app/ui/user_list/view/user_list_page.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 UserListPage(),
);
}
}CHECK 0 / 4
Motion Note Hands-on