• 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. }

  • 相关阅读:
    GCN笔记:Graph Convolution Neural Network,ChebNet
    promise defer的使用方法
    教你复制大量文件,保存到多个文件夹中
    RK3568的CAN驱动适配
    STM32的GPIO端口的八种模式解析
    AtCoder ABC132
    wordpress redirected you too many times.(多重重定向)
    安全高效,人人都是测绘师!(附教程)
    案例:用户管理
    眼底相机系统设计
  • 原文地址:https://blog.csdn.net/lebsharing/article/details/125520965