gpt4 book ai didi

flutter - 在 Flutter 中组合 reducer

转载 作者:行者123 更新时间:2023-12-04 08:37:25 25 4
gpt4 key购买 nike

我正在将 Redux 与 Flutter 与 this library 一起使用.如果解决方案是更改为 redux dart 库,那么我将更改它。
这是我为商店提供的代码,我一直在从不同的教程中学习

@immutable
class AppState {
final SignUpState signUpState;
final LoginState loginState;

AppState({
@required this.signUpState,
@required this.loginState,
});
AppState copyWith({
SignUpState signUpState,
LoginState loginState,
}) {
return AppState(
signUpState: signUpState ?? this.signUpState,
loginState: loginState ?? this.loginState,
);
}
}

AppState appReducer(AppState state, action) {
return AppState(
signUpState: signUpReducer(state.signUpState, action),
// loginState: loginReducer(state.loginState, action),
);
}

class Redux {
static Store<AppState> _store;

static Store<AppState> get store {
if (_store == null) {
throw Exception("store is not initialized");
} else {
return _store;
}
}

static Future<void> init() async {
final signUpStateInitial = SignUpState.initial();
final loginStateInitial = LoginState.initial();

_store = Store<AppState>(
appReducer,
middleware: [thunkMiddleware, new LoggingMiddleware.printer()],
initialState: AppState(
signUpState: signUpStateInitial,
loginState: loginStateInitial,
),
);
}
}
我首先使用 signUpState 进行测试,当我只使用那个时,它目前有效,当我添加另一个 reducer 并取消注释该行时
// loginState: loginReducer(state.loginState, action),
关于尝试发送与注册相关的操作 与上面注释的行一起使用 我得到 Uncaught (in promise) Error: Expected a value of type 'SetLoginStateAction', but got one of type 'SetSignUpStateAction'我认为这部分代码是结合 reducer :
AppState appReducer(AppState state, action) {  
return AppState(
signUpState: signUpReducer(state.signUpState, action),
loginState: loginReducer(state.loginState, action),
);
}
那我该怎么办?
编辑:我现在更改了这样的代码,但问题仍然存在
@immutable
// Define your State
class AppState {
final SignUpState signUpState;
final LoginState loginState;

AppState(this.signUpState, this.loginState);
}

AppState appReducer(AppState state, action) => new AppState(
signUpReducer(state.signUpState, action),
loginReducer(state.loginState, action),
);

class Redux {
static Store<AppState> _store;

static Store<AppState> get store {
if (_store == null) {
throw Exception("store is not initialized");
} else {
return _store;
}
}

static Future<void> init() async {
// print(1);
final signUpStateInitial = SignUpState.initial();
// print(2);
final loginStateInitial = LoginState.initial();
// print(3);

_store = Store<AppState>(
appReducer,
middleware: [thunkMiddleware, new LoggingMiddleware.printer()],
initialState: AppState(
signUpStateInitial,
loginStateInitial,
),
);
}
}
编辑:这是注册状态(它没有做太多,因为我只是在测试)
@immutable
class SignUpState {
final bool isError;
final bool isLoading;
final bool isLoggedIn;

SignUpState({
this.isError,
this.isLoading,
this.isLoggedIn,
});

factory SignUpState.initial() => SignUpState(
isLoading: false,
isError: false,
isLoggedIn: false,
);

SignUpState copyWith({
@required bool isError,
@required bool isLoading,
@required bool isLoggedIn,
}) {
return SignUpState (
isError: isError ?? this.isError,
isLoading: isLoading ?? this.isLoading,
isLoggedIn: isLoggedIn ?? this.isLoading,
);
}
}
还有注册缩减器
signUpReducer(SignUpState prevState, SetSignUpStateAction action) {   
final payload = action.signUpState;
print(action); // prints Instance of 'SetSignUpStateAction'
print(prevState); // prints Instance of 'SignUpState'
print("signUpReducer");
return prevState.copyWith(
isError: payload.isError,
isLoading: payload.isLoading,
isLoggedIn: payload.isLoggedIn,
);
}
打印语句执行
编辑2:
这是登录 reducer ,但这里还没有进行任何操作,我没有实现任何登录操作
loginReducer(LoginState prevState, SetLoginStateAction action) {
final payload = action.loginState;

return prevState.copyWith(
isError: payload.isError,
isLoading: payload.isLoading,
);
}

最佳答案

您调度的操作将被发送到所有 reducer 。使用检查,您可以轻松地改变您的状态。这也将防止状态中不必要的更新。
很快你就会有多个 Action 被分派(dispatch)并按如下方式处理它们将允许你这样做。
您的 signUpReducer应该是这样的:

signUpReducer(SignUpState prevState, dynamic action) {

if (action is SetSignUpStateAction) {
return setSignUpState(prevState, action);
}
return prevState;
}

SignUpState setSignUpState(SignUpState prevState, SetSignUpStateAction action) {
final payload = action.signUpState;
print(action); // prints Instance of 'SetSignUpStateAction'
print(prevState); // prints Instance of 'SignUpState'
print("signUpReducer");
return prevState.copyWith(
isError: payload.isError,
isLoading: payload.isLoading,
isLoggedIn: payload.isLoggedIn,
);
}
您的 loginReducer应该是这样的:
loginReducer(LoginState prevState, dynamic action) {

if(action is SetLoginStateAction) {
return setLoginState(prevState, action);
}
return prevState;
}

LoginState setLoginState(LoginState prevState, SetLoginStateAction action) {
final payload = action.loginState;

return prevState.copyWith(
isError: payload.isError,
isLoading: payload.isLoading,
);
}

关于flutter - 在 Flutter 中组合 reducer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64735206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com