0

我有一个从 ChangeNotifier 扩展的类,它管理一个小部件的状态:

  MainSection _section = MainSection.SETUP;

  MainSection get section => _section;

  set section(MainSection value) {
    _section = value;

    // some code

    notifyListeners();
  }

正如我所说,我使用它来更改小部件的状态:

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MainBloc>.value(
      value: _bloc,
      child: Consumer<MainBloc>(builder: (context, bloc, child) {
        _bloc = bloc;
        var body;

        switch (_bloc.section) {
          case MainSection.SETUP:
            body = _widgetFactory.createSetupWidget();
            break;
          case MainSection.WAITING:
            body = Column(
              children: <Widget>[
                Expanded(
                  child: _widgetFactory.createWaitingWidget(),
                ),
                _getBottomBar()
              ],
            );
            break;

这种机制运行良好,因为我更新了应用程序以使用最新的 Flutter 版本。现在在调试模式下,它在所有情况下都可以正常工作,但在配置文件或发布模式下,它在应用程序中的特定点不起作用,这意味着它适用于某些状态更改,但对于特定更改不起作用。我不知道有什么影响。

为什么我说它不起作用:我更改了变量,调用 notifyListeners() 但消费者没有得到通知。

我正在使用提供程序依赖版本 4.3.1

颤振医生:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.5, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2019.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
[!] VS Code (version 1.47.3)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)
4

2 回答 2

0

我已经发现发生了什么,一些子小部件正在构建方法中处理未来:

  @override
  Widget build(BuildContext context) {
    return FutureProvider<FutureBundle>.value(
        value: _bloc.getChannels(),
        initialData: FutureBundle(state: BundleState.LOADING),
        catchError: (context, error) {
          return FutureBundle(state: BundleState.ERROR, data: error);
        },
        child: Consumer<FutureBundle>(builder: (context, bundle, view) {

我在此响应参考之后更改了此实现,并且一切都再次正常工作:

@override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: future,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return ChangeNotifierProvider<WaitingBloc>.value(
              value: _bloc,
              child: Consumer<WaitingBloc>(builder: (context, bloc, child) {
于 2020-08-06T14:50:10.990 回答
0

我知道这是一个老问题,它已经有一个正确的答案,但如果这对任何人都有帮助。

同样的错误发生在我身上,因为(基本上)我是从小部件树下某处的方法调用notifyListeners()build。删除该调用解决了该问题。

不知道为什么我的代码在调试模式下工作。

于 2021-10-11T23:46:06.420 回答