问题
Is it possible to get an up-to-date state from the Store outside the Widget tree?
In Provider, you can easily do that by calling Provider like this and the state will update if there is any change.
Provider.of<WelcomeBloc>(context)
But when I call:
StoreProvider.of<AppState>(context).state.currentPage;
I just get the current state but it doesn't update on changes (or I am doing something wrong there)
In that case, I need to use that call in a block which updates on changes... what in my example is not a case
example
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:ui_flutter/model/app_state.dart';
import './footer.dart';
import './viewWrapper.dart';
import './header.dart';
class Welcome extends StatefulWidget {
@override
_WelcomeState createState() => _WelcomeState();
}
class _WelcomeState extends State<Welcome> {
@override
Widget build(BuildContext context) {
int currentPage = StoreProvider.of<AppState>(context).state.currentPage; // This build method is invoked once only so I don't have update data
print('current page: $currentPage');
return Scaffold(
body: StoreConnector<AppState, AppState>(
converter: (store) => store.state,
builder: (context, state) {
return SafeArea(
child: Stack(
children: <Widget>[
ViewerWrapper(),
Footer(
currentStep: state.currentPage,
totalSteps: 3,
activeColor: Colors.grey[800],
inactiveColor: Colors.grey[100],
),
WelcomeHeader,
],
),
);
},
),
);
}
}
Thanks in advance!
来源:https://stackoverflow.com/questions/61611230/get-updated-state-outside-a-widget-tree-with-redux