简体   繁体   English

在构建过程中调用 setState() 或 markNeedsBuild()...失败的断言:第 4134 行 pos 12:“!_debugLocked”:不是真的

[英]setState() or markNeedsBuild() called during build…Failed assertion: line 4134 pos 12: '!_debugLocked': is not true

Very new to flutter and I get that type error.颤振非常新,我得到了那种类型的错误。 I build simple meals app .I don't know solution我构建了简单的膳食应用程序。我不知道解决方案

I add simple Drawer() and in this route then get error.我添加了简单的 Drawer(),然后在这条路线中出现错误。 I tried all online solution but never get solution我尝试了所有在线解决方案,但从未得到解决方案

When I open Drawer suddenly get That type error I don't Know solution.当我打开抽屉时突然得到那个类型的错误我不知道解决方案。

please give me Solution请给我解决方案

import 'package:flutter/material.dart';
import 'package:recipes/screens/filters_screen.dart';

class MainDrawer extends StatelessWidget {
  const MainDrawer({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget buildListTile(String title, IconData icon, Function tapHandler) {
      return ListTile(
          leading: Icon(
            icon,
            size: 26,
          ),
          title: Text(
            title,
            style: TextStyle(
                fontFamily: 'RobotoCondensed',
                fontSize: 24,
                fontWeight: FontWeight.bold),
          ),
          onTap: tapHandler());
    }

    return Drawer(
        child: Column(
      children: [
        Container(
          height: 120,
          width: double.infinity,
          padding: EdgeInsets.all(20),
          alignment: Alignment.centerLeft,
          color: Theme.of(context).accentColor,
          child: Text(
            'Cooking up!',
            style: TextStyle(
                fontWeight: FontWeight.w900,
                fontSize: 30,
                color: Theme.of(context).primaryColor),
          ),
        ),
        SizedBox(
          height: 20,
        ),
        buildListTile('Meals', Icons.restaurant, () {
          Navigator.of(context).pushNamed('/');
        }),
        buildListTile('Filters', Icons.settings, () {
          Navigator.of(context).pushNamed(FilterScreen.routeName);
        }),
      ],
    ));
  }
}

It's because your buildListTile widget rebuilt recursively each time you tapping the menu item.这是因为每次点击菜单项时,您的buildListTile小部件buildListTile递归重建。 So, try moving your buildListTile method from build method.因此,尝试将buildListTile方法从build方法移动。

your class structure should be something like this:你的类结构应该是这样的:

class MainDrawer extends StatelessWidget {
  const MainDrawer({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    

    return Drawer(
         ....
    );
  }

  Widget buildListTile(String title, IconData icon, Function tapHandler) {
      return ListTile(
         ...
      );
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 1-setState() 或 markNeedsBuild() 在构建期间调用。 2-断言失败:第 4165 行第 12 行:':_debugLocked':不正确 - 1-setState() or markNeedsBuild() called during build. 2-Failed assertion: line 4165 pos 12: '!_debugLocked': is not true flutter 升级后:':_debugLocked'。 不正确:-未处理的异常:在构建期间调用了 setState() 或 markNeedsBuild() - dev - after flutter upgrade: '!_debugLocked': is not true. - Unhandled Exception: setState() or markNeedsBuild() called during build - dev 获取小部件库捕获的异常和失败的断言:第 3289 行 pos 12: ':_debugLocked': is not true - Getting EXCEPTION CAUGHT BY WIDGETS LIBRARY & Failed assertion: line 3289 pos 12: '!_debugLocked': is not true 颤振错误状态:未来已完成且断言失败:第 2113 行 pos 12:'!_debugLocked':不正确 - flutter Bad state: Future already completed and Failed assertion: line 2113 pos 12: '!_debugLocked': is not true 未处理的异常:'package:flutter/src/widgets/navigator.dart':断言失败:第 3499 行 pos 12:'!_debugLocked':不正确。”FLUTTER - Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3499 pos 12: '!_debugLocked': is not true." FLUTTER 'package:flutter/src/widgets/navigator.dart':断言失败:第 5338 行 pos 12:':_debugLocked'。 不是真的。 这是我在下面给出的代码 - 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5338 pos 12: '!_debugLocked': is not true. And this is my Code given Below 错误:_AssertionError('package:flutter/src/widgets/navigator.dart':断言失败:第 4893 行 pos 12:':_debugLocked'。不是真的。) - Error: _AssertionError ('package:flutter/src/widgets/navigator.dart': Failed assertion: line 4893 pos 12: '!_debugLocked': is not true.) 在构建 Builder 时抛出了以下断言:在构建过程中调用了 setState() 或 markNeedsBuild()? - The following assertion was thrown building Builder: setState() or markNeedsBuild() called during build? 抛出另一个异常:'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2216 pos 12: ':_debugLocked': is not true - Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2216 pos 12: '!_debugLocked': is not true 在构建期间调用 setState() 或 markNeedsBuild - setState() or markNeedsBuild called during build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM