An open API service indexing awesome lists of open source software.

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

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()
),
);
```