I have the need to pass the redux store state from a screen to another inside the build
function.
My problem is that in the build
function I have the ViewModel variable, that does not have a reference to the state.
This is the code of the screen:
import ...
class Menu extends StatefulWidget {
@override
_MenuState createState() => _MenuState();
}
class _MenuState extends State<Menu> {
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModelLogin>(
converter: (store) => ViewModelLogin.create(store),
builder: (context, ViewModelLogin viewModel) {
Widget _buildPage(isLoggedIn) {
if (isLoggedIn) {
return ListView(
children: <Widget>[
ListTile(
title: Text('Settings'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MySettingsScreen(), // <-- HERE I NEED TO PASS THE STATE TO MySettingsScreen
),
);
},
),
ListTile(
leading: Image.network(
viewModel.loginType == 'facebook'
? 'https://img.icons8.com/color/52/000000/facebook.png'
: 'https://image.flaticon.com/teams/slug/google.jpg'
,
width: 30.0,
),
title: Text('Exit'),
onTap: () {
viewModel.onLogout(viewModel.loginType);
}
),
],
);
} else {
return LoginScreen(appBar: false);
}
}
return _buildPage(viewModel.isLoggedIn);
},
);
}
}
The reason I need to pass the state to MySettingsScreen is that in the screen I need a store variable to do a get call to a webservice (outside the build function).
This is a part of MySettingsScreen where I need the store state:
import ...
class MySettingsScreen extends StatefulWidget {
final AppState state;
MySettingsScreen({Key key, @required this.state}) : super(key: key);
@override
_MySettingsScreenState createState() => _MySettingsScreenState();
}
class _MySettingsScreenState extends State<MySettingsScreen> {
@override
void initState() {
super.initState();
_load();
}
void _load() async {
final url = 'url';
try {
http.Response res = await http.get(url, headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + // <-- HERE I NEED THE STORE VARIABLE
});
final data = json.decode(res.body);
tmpItems = _parseItems(data["bookings"]);
} catch(e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
...
}
}