a687e3637f
Migrate StateNotifierProvider/StateNotifier → NotifierProvider/Notifier, StateProvider → NotifierProvider with simple notifier, FamilyAsyncNotifier removed in Riverpod 3 (replaced with constructor-arg pattern), and rename .valueOrNull → .value throughout all providers and screens. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
64 lines
1.6 KiB
Dart
64 lines
1.6 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../data/models/note.dart';
|
|
import 'api_client_provider.dart';
|
|
|
|
final notesProvider =
|
|
AsyncNotifierProvider<NotesNotifier, List<Note>>(NotesNotifier.new);
|
|
|
|
class NotesNotifier extends AsyncNotifier<List<Note>> {
|
|
@override
|
|
Future<List<Note>> build() async {
|
|
return ref.watch(notesRepositoryProvider).getAll();
|
|
}
|
|
|
|
Future<Note> create(
|
|
String title,
|
|
String body, {
|
|
List<String> tags = const [],
|
|
int? projectId,
|
|
}) async {
|
|
final note = await ref
|
|
.read(notesRepositoryProvider)
|
|
.create(title, body, tags: tags, projectId: projectId);
|
|
state = AsyncData([...state.value ?? [], note]);
|
|
return note;
|
|
}
|
|
|
|
Future<Note> updateNote(
|
|
int id,
|
|
String title,
|
|
String body, {
|
|
List<String> tags = const [],
|
|
int? projectId,
|
|
bool clearProject = false,
|
|
}) async {
|
|
final updated = await ref.read(notesRepositoryProvider).update(
|
|
id,
|
|
title,
|
|
body,
|
|
tags: tags,
|
|
projectId: projectId,
|
|
clearProject: clearProject,
|
|
);
|
|
state = AsyncData([
|
|
for (final n in state.value ?? [])
|
|
if (n.id == id) updated else n,
|
|
]);
|
|
return updated;
|
|
}
|
|
|
|
Future<void> delete(int id) async {
|
|
await ref.read(notesRepositoryProvider).delete(id);
|
|
state = AsyncData([
|
|
for (final n in state.value ?? [])
|
|
if (n.id != id) n,
|
|
]);
|
|
}
|
|
}
|
|
|
|
final noteDetailProvider =
|
|
FutureProvider.family<Note, int>((ref, id) async {
|
|
return ref.watch(notesRepositoryProvider).getOne(id);
|
|
});
|