Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

读取本地缓存的账号密码信息赋值给TextEditingController会报错 #268

Closed
Forwyn opened this issue May 30, 2019 · 3 comments
Closed

Comments

@Forwyn
Copy link

Forwyn commented May 30, 2019

context.dispatch(LoginActionCreator.accountAction(await SharedPreferenceUtil.getString('name'),await SharedPreferenceUtil.getString('pass')));

LoginState _account(LoginState state, Action action) {
  final Map<String, String> update = action.payload ?? <String, String>{};
  final LoginState newState = state.clone();
  newState.nameEditingController.text = update['name'] ?? state.name;
  newState.passEditingController.text = update['pass'] ?? state.pass;
  newState.name = update['name'] ?? state.name;
  newState.pass = update['pass'] ?? state.pass;
  return newState;
}

I/flutter ( 6466): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
I/flutter ( 6466): The following ArgumentError was thrown while dispatching notifications for TextEditingController:
I/flutter ( 6466): Invalid argument(s): Reducers may not dispatch actions.
I/flutter ( 6466): When the exception was thrown, this was the stack:
I/flutter ( 6466): #0      _throwIfNot (package:fish_redux/src/redux/create_store.dart:11:5)
I/flutter ( 6466): #1      _createStore.<anonymous closure> (package:fish_redux/src/redux/create_store.dart:35:7)
I/flutter ( 6466): #2      Logic._applyOnAction.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:fish_redux/src/redux_component/logic.dart:139:15)
I/flutter ( 6466): #3      DefaultContext.dispatch (package:fish_redux/src/redux_component/context.dart:59:38)
@hzgotb
Copy link
Collaborator

hzgotb commented May 30, 2019

Invalid argument(s): Reducers may not dispatch actions。无效参数。你排查一下。

@zjuwjf
Copy link
Contributor

zjuwjf commented May 30, 2019

原因:
猜测nameEditingController 或者 passEditingController 里有有注册监听:当text发生变化时,会触发reducer。
所以当
newState.nameEditingController.text = update['name'] ?? state.name;
newState.passEditingController.text = update['pass'] ?? state.pass;

使得,在_account reducer进行过程内部,再次触发nameEditingController内部监听的reducer,导致异常。

真确的做法:

LoginState _account(LoginState state, Action action) {
  final Map<String, dynamic> update = action.payload ?? <String, dynamic>{};
  final LoginState newState = state.clone();
  newState.nameEditingController = update['nameEditingController'] ;
  newState.passEditingController= update['passEditingController'];
  newState.name = update['name'] ?? state.name;
  newState.pass = update['pass'] ?? state.pass;
  return newState;
}

其中update['nameEditingController']是新创建的controller

或者在nameEditingController内部监听中延迟一帧发送action。

@Forwyn
Copy link
Author

Forwyn commented May 30, 2019

context.dispatch(LoginActionCreator.accountAction(await SharedPreferenceUtil.getString('name'),await SharedPreferenceUtil.getString('pass')));
context.state.nameEditingController.addListener((){
context.dispatch(LoginActionCreator.updateAction(context.state.nameEditingController.text,null));
});
context.state.passEditingController.addListener((){
context.dispatch(LoginActionCreator.updateAction(null, context.state.passEditingController.text));
});
感谢大佬指点,把监听加在后面,就不报错了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants