Comments (4)
Two StoreProviders ... It was late at night when I cut and paste that after much trial and error, so I pasted in bad sample code. I wasn't typing my StoreProvider though, so I'll try that in my real app and see how that goes.
from flutter_redux.
Heya @mjohnsullivan, thanks for the report. Could you please let me know which version of Flutter you're on (beta, master, etc)? I fear I started using Dart 2 features before they're stable :(
from flutter_redux.
Thanks for providing your code! Ok, so I think what's happening is you're running into some expected Dart 2 behavior, but perhaps Dart 2 isn't complaining loudly enough, or perhaps some of the type inference should be working but isn't. I'm going to throw a more helpful error from this lib in these cases.
I noticed a couple of things:
- You've got 2
StoreProviders
. You only need 1StoreProvider
at the root of your app. It turns out this is not the problem. - If you add some explicit typing, Dart 2 will begin to work. If you change this line:
final store = new Store(counterReducer, initialState: 1);
to this:final Store store = new Store<int>(counterReducer, initialState: 1);
, your example should being to work (this is essentially what the first example was doing), even with 2 StoreProviders. In fact, my IDE was complaining aboutcounterReducer
not being a function of the correct type without the generic type info. In this case, you're essentially casting aStore<int>
to something with less type info and passing it to a StoreProvider that has no generic type info. This means allStoreConnector
definitions must be used without generic type info as well, or you'd run into the problem again.
Overall, I'd highly recommend specifying the generic types of all classes in Dart 2: List<int>
rather than List
, Store<int>
over Store
, etc. Otherwise I've experience a lot of pain with Dart 2, as it handles these cases differently than it did in Dart 1.
Some more info: this problem boils down to one function call: inheritFromWidgetOfExactType. While most calls to this method in Flutter do not require Generic type info, e.g. (Theme.of(context)
), the StoreProvider
store does contain generic type info (StoreProvider<S>
). In these case, things get a bit hairy if full type info isn't provided.
Second example with slightly more info:
enum Actions { Increment }
// The reducer, which takes the previous count and increments it in response
// to an Increment action.
int counterReducer(int state, dynamic action) {
if (action == Actions.Increment) {
return state + 0;
}
return state;
}
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
final Store store = new Store<int>(counterReducer, initialState: 1);
@override
Widget build(BuildContext context) {
return StoreProvider(
store: store,
child: new MaterialApp(
title: 'Flutter',
home: new Center(
child: new StoreConnector(
converter: (store) => store.state.toString(),
builder: (context, count) {
return new Text('$count');
},
),
),
),
);
}
}
Recommended way of writing with Generic type info
enum Actions { Increment }
// The reducer, which takes the previous count and increments it in response
// to an Increment action.
int counterReducer(int state, dynamic action) {
if (action == Actions.Increment) {
return state + 0;
}
return state;
}
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
final store = new Store<int>(counterReducer, initialState: 1);
@override
Widget build(BuildContext context) {
return StoreProvider<int>(
store: store,
child: new MaterialApp(
title: 'Flutter',
home: new Center(
child: new StoreConnector<int, String>(
converter: (store) => store.state.toString(),
builder: (context, count) {
return new Text('$count');
},
),
),
),
);
}
}
from flutter_redux.
Hey @mjohnsullivan, gonna close this one down in favor of the other issues filed on this repo around Dart 2 support :)
Please let me know if you run into any other bugs!
from flutter_redux.
Related Issues (20)
- How to not accept change notifications when the Router is not at the top level, and then take the initiative to obtain the changes when the Router returns to the top level HOT 1
- onDidChange called twice HOT 8
- Is this package still actively maintained?
- `onWillChange` and `onDidChange` errors are swallowed HOT 4
- Warning related to `?` operator on Linux/Dev channel. HOT 2
- Import Store data type
- Widgets binding error after upgrading flutter version to 2.13.0-0.1.pre HOT 1
- onDidChange not working as expected HOT 3
- Build warning with Flutter 3.0 HOT 5
- flutter_redux depends on flutter_redux, version solving failed HOT 3
- onWillChange and onDidChange called twice after state update HOT 2
- Passing Store as an argument to MethodChannel and Background Isolate
- Question on how to use the reducer right way HOT 2
- [Question]: Any thoughts on difference between ignoreChange and distinct?
- Socket and Flutter_redux HOT 18
- Dispatching an action that does not alter state rerenders the widget, causing an infinite loop HOT 5
- How to watch redux store change inside useEffect? HOT 5
- [Question] is it expected that the store doesn't run until frames render? HOT 1
- After push a new page, and Dispatching an action, there is an infinite loop
- Is flutter redux actively maintained HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_redux.