You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter模拟器报Widgets Library异常:owner断言失败

问题原因与修复方案

核心错误原因

你代码里的TodoApp组件在build方法中,把MaterialApp的home属性设置为TodoApp(),这会导致无限递归创建TodoApp实例——每次构建TodoApp,都会再创建一个新的TodoApp作为首页,循环往复触发框架的断言检查失败,也就是你看到的owner!._debugCurrentBuildTarget == this错误。

修复步骤

修改TodoApp的build方法,把home替换成你注释掉的TodoList组件:

class TodoApp extends StatelessWidget {
  const TodoApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Todo Uygulaması',
        theme: ThemeData(
          primarySwatch: Colors.pink,
        ),
        home: TodoList(title: 'Todo Uygulaması') // 替换原来的TodoApp()
        );
  }
}

额外优化建议(可选)

  • 避免使用全局变量TextEditingController t1和List TodoVerileri,建议把它们移到_TodoListState内部,封装在状态类里更符合Flutter的状态管理规范,也能避免全局状态污染。
  • AlertDialog的actions属性应放置按钮类组件,你把TextField放在actions里不符合组件规范,建议移到content属性中:
    builder: (ctx) => AlertDialog(
          title: const Text("Type Your Task Here"),
          content: TextField(controller: t1), // 移到content区域
          actions: [
            TextButton(
                onPressed: () {
                  Navigator.pop(context);
                  veriEkle();
                },
                child: Text('Create')),
            TextButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text('Cancel'))
          ],
        )
    

内容的提问来源于stack exchange,提问作者TheAura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:55:11