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

Access store outside flutter widget tree? #13

Closed
srtonz opened this issue Mar 4, 2018 · 3 comments
Closed

Access store outside flutter widget tree? #13

srtonz opened this issue Mar 4, 2018 · 3 comments

Comments

@srtonz
Copy link

srtonz commented Mar 4, 2018

I'm having issues accessing the store from outside the flutter widget tree, and I'm not quite sure if the problem lies with me or if it's simply not possible.

Say I define a store in my main.dart:

Store<AppState> store = new Store(counterReducer, initialState: new myAppState());

class FlutterReduxApp extends StatelessWidget {
  // Create your store as a final variable in a base Widget. This works better
  // with Hot Reload than creating it directly in the `build` function.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData.dark(),
      home: new StoreProvider(
        store: store,
....

Then modify the state in the StoreProvider's descendants and try to access it:

@override build(BuildContext context) {
  String attrA = new StoreProvider.of(context).store.state.myAttribute;
  String attrB = store.state.myAttribute; // Direct access `store` from `main.dart`
  assert(attrA == attrB); // Fails
}

In the example above attrA has the most recent state, but attrB seems to have the store's initial state. Am I making a mistake somewhere or is this a limitation of flutter_redux?

The reason I'd like to get the store singleton outside the widget tree is to add extra store-based functionality to my models, e.g. searching the store for related models etc...

@brianegan
Copy link
Owner

brianegan commented Mar 4, 2018

Yep! That's totally possible.

You're most likely running into a confusing issue with Dart: If you declare a global variable in your main.dart file and use it internally it will create an instance. Then, if you import that main.dart file using import 'package:my_app/main.dart in another file in your app and try to use the variable, the Singleton will be recreated.

Instead, you can move that store declaration to it's own file, and import that in your main and your other files, you should be all good. Don't ask me why :) 👻 👻 👻

Let me know if that works for ya!

@srtonz
Copy link
Author

srtonz commented Mar 4, 2018

sigh thanks mate, and here I was thinking flutter_redux does some magic to copy the store internally... sorry about that and thanks for helping out ;-)

@yudi43
Copy link

yudi43 commented Dec 1, 2019

Damn I love this package, putting the store in different file worked!!!!

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

3 participants