Get updated State outside a Widget tree with Redux

耗尽温柔 提交于 2021-02-11 12:23:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!