繁体   English   中英

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

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

颤振非常新,我得到了那种类型的错误。 我构建了简单的膳食应用程序。我不知道解决方案

我添加了简单的 Drawer(),然后在这条路线中出现错误。 我尝试了所有在线解决方案,但从未得到解决方案

当我打开抽屉时突然得到那个类型的错误我不知道解决方案。

请给我解决方案

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);
        }),
      ],
    ));
  }
}

这是因为每次点击菜单项时,您的buildListTile小部件buildListTile递归重建。 因此,尝试将buildListTile方法从build方法移动。

你的类结构应该是这样的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM