You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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")
);
}
}
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.
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
Below is the complete code.
main.dart.zip
The text was updated successfully, but these errors were encountered: