fluttercommunity / fluttercommunity/redux.dart
Use optional type instead of dynamic for actions
- Dominant language
- Dart
- Stars
- 519
- Forks
- 57
- PR merge metrics
- No merged PRs in 30d
Description
### Proposal
It'd be great if `Store` had another generic type to be used as the `action` argument type in methods like `dispatch`, preventing non-action objects from being used as actions.
This should be non-breaking for most users, as type inference should allow `Store` constructors to omit the existing state type, and the addition of another type will only break invocations that specify it explicitly.
### Reasoning
I've separated my project into layers, like so:
Data:
`project_data`: Platform-agnostic repository implementations
Domain:
`project_domain`: Core platform-agnostic functionality, including state management
Platform:
`project_flutter`: A GUI using Flutter
`project_cli`: A CLI
Platform implementations can build a Redux store with a `buildStore` function in the domain package, passing in repository implementations to use. The domain package exports different action classes that the platform implementation can dispatch to the store. Each action class extends from an internal (non-exported) `AppAction` class.
If the `dispatch` function only accepts `AppAction` subtypes, this will prevent non-action objects being used by the platform implementations, requiring all actions to be used from the domain package.
### Workarounds
At the moment, an assert can be used, but this throws an error at runtime. A compilation error is preferred.
```dart
AppState appReducer(AppState state, dynamic action) {
assert(action is AppAction,
'The action object ($action) has an invalid type (${action.runtimeType}! It must extend AppAction.');
// ...
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Store API, especially dispatch and the other methods that take an action argument, and trace how the existing state generic and dynamic action type are inferred. Done means Store consumers can constrain actions to an AppAction subtype and invalid action objects produce a compilation error rather than relying on the reducer's runtime assert.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100