-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error with Store is defined outside main() #29
Comments
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 :( |
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:
Overall, I'd highly recommend specifying the generic types of all classes in Dart 2: 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. ( 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 infoenum 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');
},
),
),
),
);
}
} |
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. |
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! |
This is probably related to #28 ...
With Dart 2, this works great:
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() {
final store = new Store(counterReducer, initialState: 1);
runApp(new MyApp(store: store));
}
class MyApp extends StatelessWidget {
MyApp({this.store});
final Store store;
@OverRide
Widget build(BuildContext context) {
return StoreProvider(
store: store,
child: new MaterialApp(
title: 'Flutter',
home: StoreProvider(
store: store,
child: new Center(
child: new StoreConnector(
converter: (store) => store.state.toString(),
builder: (context, count) {
return new Text('$count');
}))),
),
);
}
}
But the following throws this error:
The following NoSuchMethodError was thrown building StoreConnector(dirty):
The getter '_store' was called on null.
Receiver: null
Tried calling: _store
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(counterReducer, initialState: 1);
@OverRide
Widget build(BuildContext context) {
return StoreProvider(
store: store,
child: new MaterialApp(
title: 'Flutter',
home: StoreProvider(
store: store,
child: new Center(
child: new StoreConnector(
converter: (store) => store.state.toString(),
builder: (context, count) {
return new Text('$count');
}))),
),
);
}
}
The text was updated successfully, but these errors were encountered: