【问题标题】:Flutter - Unhandled Exception: setState() called after dispose() AND _StreamBuilderBaseStateFlutter - 未处理的异常:在 dispose() 和 _StreamBuilderBaseState 之后调用 setState()
【发布时间】:2020-12-05 07:33:13
【问题描述】:

更改设备方向时出现以下错误:

E/flutter (31741): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: setState() called after dispose(): _StreamBuilderBaseState<List<ScheduledActivity>, AsyncSnapshot<List<ScheduledActivity>>>#afadb(lifecycle state: defunct, not mounted)
E/flutter (31741): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (31741): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (31741): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter (31741): #0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1204:9)
E/flutter (31741): #1      State.setState (package:flutter/src/widgets/framework.dart:1239:6)
E/flutter (31741): #2      _StreamBuilderBaseState._subscribe.<anonymous closure> (package:flutter/src/widgets/async.dart:140:9)
E/flutter (31741): #3      _StartWithValueStream.listen.<anonymous closure> (package:moor/src/utils/start_with_value_transformer.dart:49:17)
E/flutter (31741): #4      _rootRun (dart:async/zone.dart:1182:47)
E/flutter (31741): #5      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (31741): #6      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (31741): #7      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (31741): #8      _rootRun (dart:async/zone.dart:1190:13)
E/flutter (31741): #9      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (31741): #10     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (31741): #11     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (31741): #12     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (31741): #13     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (31741): 

现在,我知道about the if (mounted) setState fix - 但是,我没有在我的应用程序中使用setState,因为我使用的是bloclibrary。 另外,查看错误,我可以看到StreamBuilder&lt;List&lt;ScheduledActivity&gt;&gt;,这是导致问题的以下代码sn-ps的唯一线索:

...
  @override
  Widget build(BuildContext context) {

    return StreamBuilder<List<ScheduledActivity>>(
      stream: RepositoryProvider.of<AppDatabase>(context)
          .scheduledActivitiesDao
          .watchActivitiesByPlant(widget.plantId),
      builder: (context, snapshot) {

        if (snapshot.hasError) print('>>>> DB error - ${snapshot.error}');
        if (!snapshot.hasData) return ProgressIndicatorWidget();

        return Text('items = ${snapshot.data.length}');
      },
    );
  }
...

上面代码的两个注意事项:

  1. 上面我发布的build函数的Widget是StatefulWidget,但是,即使我将它重构为StatelessWidget,我也会遇到同样的错误
  2. 仅当我从 横向 切换到 纵向 模式时才会出现此问题。

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    似乎问题与以下事实有关,每次调用 build 方法时,我都在创建 Stream

    如何重构代码:

    1. 添加Stream&lt;List&lt;ScheduledActivity&gt;&gt; _stream;
    2. 在这里设置它的值:
      @override
      void didChangeDependencies() {
        super.didChangeDependencies();
        _stream = RepositoryProvider.of<AppDatabase>(context)
            .scheduledActivitiesDao
            .watchActivitiesByPlant(widget.plantId);
      }
    
    
    1. 替换

    RepositoryProvider.of&lt;AppDatabase&gt;context).scheduledActivitiesDao.watchActivitiesByPlant( widget.plantId),

    stream: _stream,

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 2018-08-26
      • 2019-10-28
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2023-04-08
      • 2019-07-02
      相关资源
      最近更新 更多