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

Help with routing and making store available on routes. #12

Closed
manujbahl opened this issue Feb 28, 2018 · 2 comments
Closed

Help with routing and making store available on routes. #12

manujbahl opened this issue Feb 28, 2018 · 2 comments

Comments

@manujbahl
Copy link

Need some help figuring out how to do routing with StoreConnectors.
Created a basic app with navigation to 2 screens.
Screen 2 is uses a StoreConnector while Screen3 does not. Navigation to Screen 2 fails while getting the store.

Here is the call stack of the error

The following NoSuchMethodError was thrown building StoreConnector<dynamic, Store>(dirty):
The getter 'store' was called on null.
Receiver: null
Tried calling: store
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
#1 StoreConnector.build (package:flutter_redux/flutter_redux.dart:156:44)
#2 StatelessElement.build (package:flutter/src/widgets/framework.dart:3655:28)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3602:15)

The route for Screen2 looks like this

routes: <String, WidgetBuilder>{
                '/screen2': (BuildContext context) => new StoreBuilder( builder: (context, store) {
                            return new Screen2();
                        }),

Below is the complete code.

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';

dynamic reducer(dynamic state, action) {
  return state;
}

void main() {
        runApp(new MyApp());
}

class MyApp extends StatelessWidget {
    final store = new Store(reducer, initialState: {"1" : 1, "2": 2});
    
    Widget build(BuildContext context) {
        return new MaterialApp(
            home: new StoreProvider(
                store: store,
                child: new Screen1(),
            ),
            routes: <String, WidgetBuilder>{
                '/screen2': (BuildContext context) => new StoreBuilder( builder: (context, store) {
                            return new Screen2();
                        }),
                '/screen3': (BuildContext context) => new Screen3()
            },
        );
    }
}

class Screen1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold( // 1
      appBar: new AppBar(
        title: new Text("Screen 1"), // screen title
      ),
      body: new StoreConnector<dynamic, Map>(
            converter: (store) => (store.state['1']),
            builder: (context, jsonNodeData) => new Center(
              child: new Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  new RaisedButton(
                    onPressed:(){
                      Navigator.of(context).pushNamed("/screen2");
                    },
                    child: new Text("Go to Screen 2"),
                  ),
                  new RaisedButton(
                    onPressed:(){
                      Navigator.of(context).pushNamed("/screen3");
                    },
                    child: new Text("Go to Screen 3"),
                  ),
                ]
              )
            )
          )
        );
  }
}


class Screen2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Screen 2"),
      ),
      body: new StoreConnector<dynamic, Map>(
            converter: (store) => (store.state['1']),
            builder: (context, jsonNodeData) => new Text("Screen 2")
       ) ,
    );
  }
}


class Screen3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Screen 3"),
      ),
      body: new Text("Screen 3")
    );
  }
}

main.dart.zip

@brianegan
Copy link
Owner

Hey there!

Short version: make your MaterialApp a child of the StoreProvider.

Right now, only your home route is providing the Store to it's children. To share a Redux Store or any other type of data across routes, you can lift that data above the Material app. It will then be available to all routes.

@manujbahl
Copy link
Author

Thanks.. yup that was it

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