• The widget on which setState() or markNeedsBuild() was called exception


    一、异常信息

    I/flutter (30504): [E]: message: {error: setState() or markNeedsBuild() called during build.
    I/flutter (30504): This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
    I/flutter (30504): The widget on which setState() or markNeedsBuild() was called was:
    I/flutter (30504):   Overlay-[LabeledGlobalKey<OverlayState>#b5023]
    I/flutter (30504): The widget which was currently being built when the offending call was made was:
    I/flutter (30504):   NotificationListener<KeepAliveNotification>, stackTrace: #0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4424:11)
    I/flutter (30504): #1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4439:6)
    I/flutter (30504): #2      State.setState (package:flutter/s
    I/flutter (30504): [E]: message: {error: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2845 pos 18: '!navigator._debugLocked': is not true., stackTrace: #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
    I/flutter (30504): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
    I/flutter (30504): #2      _RouteEntry.handlePush.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2845:18)
    I/flutter (30504): #3      TickerFuture.whenCompleteOrCancel.thunk (package:flutter/src/scheduler/ticker.dart:407:15)
    I/flutter (30504): #4      _rootRunUnary (dart:async/zone.dart:1434:47)
    I/flutter (30504): #5      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
    I/flutter (30504): <asynchronous suspension>
    I/flutter (30504): #6      TickerFuture.whenCompleteOrCancel.thunk (package:flutter/src/scheduler/ticker.dart:406:5)
    I/flutter (30504): <asynchronous suspension>
    I/flutter (30504): }
    [GETX] Instance "StudentRecordingController" has been created
    [GETX] Instance "StudentRecordingController" has been initialized
    I/flutter (30504): [E]: message: {exception : HttpException: Invalid statusCode: 403, uri = https://cdn.lingoace.com/avator/1925098231/2uk0cy-20210727073545, stackTrace: }
    I/HwViewRootImpl(30504): removeInvalidNode jank list is null
     

    二、产生该问题的类

    1. class HistoryRecordingWidget extends StatefulWidget {
    2. const HistoryRecordingWidget({Key? key}) : super(key: key);
    3. @override
    4. State<HistoryRecordingWidget> createState() => _HistoryRecordingWidgetState();
    5. }
    6. class _HistoryRecordingWidgetState extends State<HistoryRecordingWidget> {
    7. // TapGestureRecognizer _recognizer = TapGestureRecognizer();
    8. late TapGestureRecognizer _recognizer;
    9. @override
    10. void initState() {
    11. super.initState();
    12. _recognizer = TapGestureRecognizer();
    13. _recognizer..onTap = _handleOnTap();
    14. }
    15. _handleOnTap() {
    16. if (mounted) {
    17. }
    18. }
    19. @override
    20. void dispose() {
    21. _recognizer.dispose();
    22. super.dispose();
    23. }
    24. @override
    25. Widget build(BuildContext context) {
    26. return Container(
    27. decoration: BoxDecoration(
    28. color: Colors.white,
    29. border: Border.all(
    30. color: Colors.blue,
    31. width: 0.5,
    32. ),
    33. ),
    34. child: Text.rich(
    35. TextSpan(
    36. children: [
    37. TextSpan(
    38. text: "Student Recording tips",
    39. ),
    40. TextSpan(
    41. text: "Student Recording tips in history2",
    42. style: TextStyle(
    43. color: Color(0xFF777FDE),
    44. fontWeight: FontWeight.w400,
    45. decoration: TextDecoration.underline,
    46. ),
    47. // recognizer: _recognizer
    48. // ..onTap = () {
    49. // _handleOnTap();
    50. // }),
    51. recognizer: _recognizer),
    52. ],
    53. ),
    54. ),
    55. );
    56. }
    57. }

    三、修复方案

    1、TapGestureRecognizer 直接出实话。

    2、_recognizer.onTap 的复制使用匿名函数。并且点击事件的赋值不在intState中进行初始化。

    1. recognizer: _recognizer
    2. ..onTap = () {
    3. _handleOnTap();
    4. }),

    修复后的代码如下:

    1. class _HistoryRecordingWidgetState extends State<HistoryRecordingWidget> {
    2. TapGestureRecognizer _recognizer = TapGestureRecognizer();
    3. _handleOnTap() {
    4. if (mounted) {
    5. //to do
    6. }
    7. }
    8. @override
    9. void dispose() {
    10. _recognizer.dispose();
    11. super.dispose();
    12. }
    13. @override
    14. Widget build(BuildContext context) {
    15. return Container(
    16. decoration: BoxDecoration(
    17. color: Colors.white,
    18. border: Border.all(
    19. color: Colors.blue,
    20. width: 0.5,
    21. ),
    22. ),
    23. child: Text.rich(
    24. TextSpan(
    25. children: [
    26. TextSpan(
    27. text: "Student Recording tips",
    28. ),
    29. TextSpan(
    30. text:"Student Recording tips in history2",
    31. style: TextStyle(
    32. color: Color(0xFF777FDE),
    33. fontWeight: FontWeight.w400,
    34. decoration: TextDecoration.underline,
    35. ),
    36. recognizer: _recognizer
    37. ..onTap = () {
    38. _handleOnTap();
    39. },
    40. ),
    41. ],
    42. ),
    43. ),
    44. );
    45. }
    46. }

    四、同样的案例

    当子一个子Widget中,有点击事件处理时,但是点击处理的逻辑是通过匿名函数传递到子类的,这种情况建议传递过来的匿名函数放在匿名函数中。

    类似的代码接口如下:

    1. class TestWidget extends StatelessWidget {
    2. final VoidCallback? click;
    3. const TestWidget({Key? key,this.click}) : super(key: key);
    4. @override
    5. Widget build(BuildContext context) {
    6. return GestureDetector(
    7. onTap: (){
    8. ///在匿名函数中调用外部传递的匿名函数
    9. if(click != null) {
    10. click!();
    11. }
    12. },
    13. child: Container(
    14. color: Colors.blue,
    15. width: 100,
    16. height: 100,
    17. ),
    18. );
    19. }
    20. }

  • 相关阅读:
    使用idea搭建SpringCloud项目(及所遇到的坑)
    Day44 动态规划part04
    2-8:Http响应头content-type内容详解
    Spring AOP框架
    数据库的备份和恢复
    Harmony系统更改手机IP
    代码随想录算法训练营Day36 —— 435. 无重叠区间、763.划分字母区间、56. 合并区间
    你安全吗?丨通过IP地址如何查到实际地址?
    【面试普通人VS高手系列】Redis和Mysql如何保证数据一致性
    lotus 1.16.0 最小快照导出 导入
  • 原文地址:https://blog.csdn.net/lebsharing/article/details/125520965