Skip to content
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

Closed
mjohnsullivan opened this issue Mar 22, 2018 · 4 comments
Closed

Error with Store is defined outside main() #29

mjohnsullivan opened this issue Mar 22, 2018 · 4 comments

Comments

@mjohnsullivan
Copy link

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');
}))),
),
);
}
}

@brianegan
Copy link
Owner

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 :(

@brianegan
Copy link
Owner

brianegan commented Mar 22, 2018

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:

  1. You've got 2 StoreProviders. You only need 1 StoreProvider at the root of your app. It turns out this is not the problem.
  2. 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 about counterReducer not being a function of the correct type without the generic type info. In this case, you're essentially casting a Store<int> to something with less type info and passing it to a StoreProvider that has no generic type info. This means all StoreConnector 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');
            },
          ),
        ),
      ),
    );
  }
}

@mjohnsullivan
Copy link
Author

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.

@brianegan
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants