如何获取我的代码(使用 Freezed 的颤振)来使用“copyWith”冻结功能来更新状态?
冻结类:
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'todo_list.freezed.dart';
part 'todo_list.g.dart';
@freezed
class TodoList with _$TodoList {
const TodoList._();
factory TodoList({ required List<String> titles }) = _TodoList;
factory TodoList.loading() = Loading;
factory TodoList.error([String? message]) = ErrorDetails;
factory TodoList.fromJson(Map<String, dynamic> json) => _$TodoListFromJson(json);
}
状态通知器
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gc_todo/presentation/state/todo_list.dart';
import 'dart:developer' as developer;
final todoListProvider = StateNotifierProvider((ref) => TodoListNotifier());
class TodoListNotifier extends StateNotifier<TodoList> {
TodoListNotifier() : super( TodoList(titles: ["first", "second"]) );
void addTodoList(String newTitle) {
developer.log("addTodoList - $state");
if (state is List<String>) (List<String> stateToUse) {
stateToUse.copyWith(); // STILL DOES NOT RECOGNISE "copyWith"
};
}
}