https://github.com/dabit3/flutter-redux-example
Example app using Flutter with Redux including multiple reducers & initial app state
https://github.com/dabit3/flutter-redux-example
dart flutter flutter-dart redux redux-dart
Last synced: about 1 month ago
JSON representation
Example app using Flutter with Redux including multiple reducers & initial app state
- Host: GitHub
- URL: https://github.com/dabit3/flutter-redux-example
- Owner: dabit3
- Created: 2018-09-28T18:02:32.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-12T22:49:40.000Z (over 6 years ago)
- Last Synced: 2025-05-05T22:54:14.812Z (about 2 months ago)
- Topics: dart, flutter, flutter-dart, redux, redux-dart
- Language: Dart
- Size: 108 KB
- Stars: 29
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Flutter Redux Example
Flutter example project using redux with multiple reducers & combineReducers.
## Getting Started
To get started, clone the project:
```sh
git clone https://github.com/dabit3/flutter-redux-example.git
```## Redux files
### model.dart
```dart
import 'package:flutter/foundation.dart';class Item {
final int id;
final String body;Item({
@required this.id,
@required this.body
});
}class AppState {
final List items;
final int count;AppState({
@required this.items,
@required this.count,
});AppState.initialState()
: items = List.unmodifiable([]),
count = 0;
}
```### reducers.dart
```dart
import 'package:reduxexample/model.dart';
import 'package:reduxexample/redux/actions.dart';
import 'package:redux/redux.dart';enum Actions { Increment }
List addItemReducer(List state, action) {
return []
..addAll(state)
..add(Item(id: action.id, body: action.item));
}List removeItemReducer(List state, action) {
return List.unmodifiable(List.from(state)..remove(action.item));
}List removeItemsReducer(List state, action) {
return List.unmodifiable([]);
}final Reducer > itemsReducer = combineReducers >([
new TypedReducer, AddItemAction>(addItemReducer),
new TypedReducer, RemoveItemAction>(removeItemReducer),
new TypedReducer, RemoveItemsAction>(removeItemsReducer),
]);int incrementReducer(int state, action) {
if (action == Actions.Increment) {
return state + 1;
}
return state;
}AppState appStateReducer(AppState state, action) {
return AppState(
items: itemsReducer(state.items, action),
count: incrementReducer(state.count, action),
);
}```
### Creating the store
```dart
final Store store = Store(
appStateReducer,
initialState: AppState.initialState(),
);return StoreProvider(
store: store,
child: new MaterialApp(
title: 'Flutter App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage()
),
);
```