Skip to content

Commit 557f34c

Browse files
committed
0.4.0 - Dart2 Support with Stronger Typing
1 parent 2b31f1a commit 557f34c

14 files changed

+18044
-200
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ addons:
99
- libstdc++6
1010
- fonts-droid
1111
before_script:
12-
- git clone https://github.com/flutter/flutter.git -b alpha --depth 1
12+
- git clone https://github.com/flutter/flutter.git -b beta --depth 1
1313
- ./flutter/bin/flutter doctor
1414
script:
15-
- ./flutter/bin/flutter test --coverage --coverage-path=lcov.info
15+
- ./flutter/bin/flutter test --preview-dart-2 --coverage --coverage-path=lcov.info
1616
after_success:
1717
- bash <(curl -s https://codecov.io/bash)
1818
cache:

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 0.4.0
4+
5+
* Works with Dart 2 (no longer supports Dart 1)
6+
* Stronger Type info Required
7+
* Breaking Changes:
8+
* `StoreProvider` now requires generic type info: `StoreProvider<AppState>`
9+
* `new StoreProvider.of(context).store` is now `StoreProvider.of<AppState>(context)`
10+
311
## 0.3.5
412

513
* Bugfix: `onInit` was not called before the initial ViewModel is constructed.

README.md

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
A set of utilities that allow you to easily consume a [Redux](https://pub.dartlang.org/packages/redux) Store to build Flutter Widgets.
66

77
This package is built to work with [Redux.dart](https://pub.dartlang.org/packages/redux).
8-
8+
99
## Redux Widgets
1010

1111
* `StoreProvider` - The base Widget. It will pass the given Redux Store to all descendants that request it.
1212
* `StoreBuilder` - A descendant Widget that gets the Store from a `StoreProvider` and passes it to a Widget `builder` function.
1313
* `StoreConnector` - A descendant Widget that gets the Store from the nearest `StoreProvider` ancestor, converts the `Store` into a `ViewModel` with the given `converter` function, and passes the `ViewModel` to a `builder` function. Any time the Store emits a change event, the Widget will automatically be rebuilt. No need to manage subscriptions!
1414

15+
## Dart Support
16+
17+
* Dart 1: 0.3.x
18+
* Dart 2: 0.4.0+. See the migration guide below!
19+
1520
## Examples
1621

1722
* [Simple example](https://gitlab.com/brianegan/flutter_redux/tree/master/example) - a port of the standard "Counter Button" example from Flutter
@@ -26,17 +31,19 @@ This package is built to work with [Redux.dart](https://pub.dartlang.org/package
2631

2732
Let's demo the basic usage with the all-time favorite: A counter example!
2833

34+
Note: This example requires flutter_redux 0.4.0+ and Dart 2! If you're using Dart 1, [see the old example](https://github.com/brianegan/flutter_redux/blob/eb4289795a5a70517686ccd1d161abdb8cc08af5/example/lib/main.dart).
35+
2936
```dart
3037
import 'package:flutter/material.dart';
31-
import 'package:redux/redux.dart';
3238
import 'package:flutter_redux/flutter_redux.dart';
39+
import 'package:redux/redux.dart';
3340
3441
// One simple action: Increment
3542
enum Actions { Increment }
3643
3744
// The reducer, which takes the previous count and increments it in response
3845
// to an Increment action.
39-
int counterReducer(int state, action) {
46+
int counterReducer(int state, dynamic action) {
4047
if (action == Actions.Increment) {
4148
return state + 1;
4249
}
@@ -45,34 +52,40 @@ int counterReducer(int state, action) {
4552
}
4653
4754
void main() {
48-
runApp(new FlutterReduxApp());
55+
// Create your store as a final variable in a base Widget. This works better
56+
// with Hot Reload than creating it directly in the `build` function.
57+
final store = Store<int>(counterReducer, initialState: 0);
58+
59+
runApp(FlutterReduxApp(
60+
title: 'Flutter Redux Demo',
61+
store: store,
62+
));
4963
}
5064
5165
class FlutterReduxApp extends StatelessWidget {
52-
// Create your store as a final variable in a base Widget. This works better
53-
// with Hot Reload than creating it directly in the `build` function.
54-
final store = new Store(counterReducer, initialState: 0);
66+
final Store<int> store;
67+
final String title;
68+
69+
FlutterReduxApp({Key key, this.store, this.title}) : super(key: key);
5570
5671
@override
5772
Widget build(BuildContext context) {
58-
final title = 'Flutter Redux Demo';
59-
60-
return new MaterialApp(
61-
theme: new ThemeData.dark(),
73+
return MaterialApp(
74+
theme: ThemeData.dark(),
6275
title: title,
63-
home: new StoreProvider(
64-
// Pass the store to the StoreProvider. Any descendant `StoreConnector`
76+
home: StoreProvider<int>(
77+
// Pass the store to the StoreProvider. Any ancestor `StoreConnector`
6578
// Widgets will find and use this value as the `Store`.
6679
store: store,
67-
child: new Scaffold(
68-
appBar: new AppBar(
69-
title: new Text(title),
80+
child: Scaffold(
81+
appBar: AppBar(
82+
title: Text(title),
7083
),
71-
body: new Center(
72-
child: new Column(
84+
body: Center(
85+
child: Column(
7386
mainAxisAlignment: MainAxisAlignment.center,
7487
children: [
75-
new Text(
88+
Text(
7689
'You have pushed the button this many times:',
7790
),
7891
// Connect the Store to a Text Widget that renders the current
@@ -90,7 +103,7 @@ class FlutterReduxApp extends StatelessWidget {
90103
// count. No need to manually manage subscriptions or Streams!
91104
new StoreConnector<int, String>(
92105
converter: (store) => store.state.toString(),
93-
builder: (context, count) => new Text(
106+
builder: (context, count) => Text(
94107
count,
95108
style: Theme.of(context).textTheme.display1,
96109
),
@@ -109,11 +122,11 @@ class FlutterReduxApp extends StatelessWidget {
109122
// with no parameters. It only dispatches an Increment action.
110123
return () => store.dispatch(Actions.Increment);
111124
},
112-
builder: (context, callback) => new FloatingActionButton(
125+
builder: (context, callback) => FloatingActionButton(
113126
// Attach the `callback` to the `onPressed` attribute
114127
onPressed: callback,
115128
tooltip: 'Increment',
116-
child: new Icon(Icons.add),
129+
child: Icon(Icons.add),
117130
),
118131
),
119132
),
@@ -123,7 +136,14 @@ class FlutterReduxApp extends StatelessWidget {
123136
}
124137
```
125138

126-
### Purpose
139+
## Dart 2 Migration
140+
141+
Dart 2 requires more strict typing (yay!), and gives us the option to make getting the Store from the StoreProvider more convenient!
142+
143+
1. Change `new StoreProvider(...)` to `StoreProvider<StateClass>(...)` in your Widget tree.
144+
2. Change `new StoreProvider.of(context).store` to `StoreProvider.of<StateClass>(context)` if you're directly fetching the `Store<AppState>` yourself from the `StoreProvider<AppState>`. No need to access the `store` field directly any more since Dart 2 can now infer the proper type with a static function :)
145+
146+
## Purpose
127147

128148
One question that [reasonable people might ask](https://www.reddit.com/r/FlutterDev/comments/6vscdy/a_set_of_utilities_that_allow_you_to_easily/dm3ll7d/): Why do you need all of this if `StatefulWidget` exists?
129149

analysis_options.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
analyzer:
2-
strong-mode: true
2+
strong-mode:
3+
implicit-casts: false
4+
implicit-dynamic: false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"fonts":[{"asset":"fonts/MaterialIcons-Regular.ttf"}],"family":"MaterialIcons"}]

0 commit comments

Comments
 (0)