IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> The widget on which setState() or markNeedsBuild() was called exception -> 正文阅读

[移动开发]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
?

二、产生该问题的类


class HistoryRecordingWidget extends StatefulWidget {
  const HistoryRecordingWidget({Key? key}) : super(key: key);

  @override
  State<HistoryRecordingWidget> createState() => _HistoryRecordingWidgetState();
}

class _HistoryRecordingWidgetState extends State<HistoryRecordingWidget> {
  // TapGestureRecognizer _recognizer = TapGestureRecognizer();
  late TapGestureRecognizer _recognizer;

  @override
  void initState() {
    super.initState();
    _recognizer = TapGestureRecognizer();
    _recognizer..onTap = _handleOnTap();
  }

  _handleOnTap() {
    if (mounted) {

    }
  }

  @override
  void dispose() {
    _recognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(
          color: Colors.blue,
          width: 0.5,
        ),
      ),     
      child: Text.rich(
        TextSpan(
          children: [
            TextSpan(
              text: "Student Recording tips",
            ),
            TextSpan(
                text: "Student Recording tips in history2",
                style: TextStyle(
                  color: Color(0xFF777FDE),
                  fontWeight: FontWeight.w400,
                  decoration: TextDecoration.underline,
                ),
                // recognizer: _recognizer
                //   ..onTap = () {
                //     _handleOnTap();
                //   }),
                recognizer: _recognizer),
          ],
        ),
      ),
    );
  }
}

三、修复方案

1、TapGestureRecognizer 直接出实话。

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

recognizer: _recognizer
                  ..onTap = () {
                    _handleOnTap();
                  }),

修复后的代码如下:

class _HistoryRecordingWidgetState extends State<HistoryRecordingWidget> {
  TapGestureRecognizer _recognizer = TapGestureRecognizer();

  _handleOnTap() {
    if (mounted) {
        //to do 
    }
  }

  @override
  void dispose() {
    _recognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(
          color: Colors.blue,
          width: 0.5,
        ),
      ),
      child: Text.rich(
        TextSpan(
          children: [
            TextSpan(
              text: "Student Recording tips",
            ),
            TextSpan(
              text:"Student Recording tips in history2",
              style: TextStyle(
                color: Color(0xFF777FDE),
                fontWeight: FontWeight.w400,
                decoration: TextDecoration.underline,
              ),
              recognizer: _recognizer
                ..onTap = () {
                  _handleOnTap();
                },
            ),
          ],
        ),
      ),
    );
  }
}

四、同样的案例

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

类似的代码接口如下:

class TestWidget extends StatelessWidget {
  final VoidCallback? click;
  const TestWidget({Key? key,this.click}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        ///在匿名函数中调用外部传递的匿名函数
        if(click != null) {
          click!();
        }
      },
      child: Container(
        color: Colors.blue,
        width: 100,
        height: 100,
      ),
    );
  }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-07-03 10:57:45  更:2022-07-03 10:58:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 4:06:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码