flutter 水合肘不储存状态

5n0oy7gb  于 2023-03-04  发布在  Flutter
关注(0)|答案(1)|浏览(112)

在下面的肘节中,我试图保持应用程序的导航状态,但当杀死设备上的应用程序并返回到程序时,它返回到默认状态。我该如何解决这个问题?
我的腕尺是:

class NavigationCubit extends HydratedCubit<NavigationState> {
  NavigationCubit() : super(NavigationState.empty);

  @override
  NavigationState fromJson(Map<String, dynamic> json) =>
      NavigationState.fromJson(json);

  @override
  Map<String, dynamic> toJson(NavigationState state) => state.toJson();
}

州代码为:

@JsonSerializable()
class NavigationState {
  final PagesTitle currentPage;
  final PagesTitle targetPage;

  const NavigationState({required this.currentPage, required this.targetPage});

  Map<String, dynamic> toJson() => _$NavigationStateToJson(this);

  factory NavigationState.fromJson(Map<String, dynamic> json) =>
      _$NavigationStateFromJson(json);

  static const NavigationState empty = NavigationState(
    currentPage: PagesTitle.dashboard,
    targetPage: PagesTitle.unknown,
  );

}

enum PagesTitle { chart, payout, dashboard, profile, setting, unknown }
eanckbw9

eanckbw91#

尝试使用HydratedMixin而不是HydratedCubit

class NavigationCubit extends Cubit< NavigationState> with HydratedMixin {
  NavigationCubit() : super(Navigationitial());

  void setNavigationState({required UserData userData}) {
    emit( NavigationState(userData: userData.toJson()));
  }

  @override
  NavigationState? fromJson(Map<String, dynamic> json) {
    return NavigationState.fromMap(json);
  }

  @override
  Map<String, dynamic>? toJson(NavigationState state) {
    return state.toMap();
  }
}

相关问题