【问题标题】:flutter setState isn't defined颤振 setState 未定义
【发布时间】:2020-06-08 13:29:52
【问题描述】:

我有一个小部件来创建侧边栏菜单:

Widget dashboard(context) {
  return Material(
    elevation: 8,
    color: backgroundColor,
    child: Container(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 48),
      child: Column(
        children: <Widget>[
          Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
            InkWell(child: Icon(Icons.menu, color: Colors.white), onTap: () {
              setState(() {
                isCollapsed = !isCollapsed;
              });
            },),
            Text("Corona App", style: TextStyle(fontSize: 24.0, color: Colors.white)),
                Icon(Icons.settings, color: Colors.white),
          ])
        ],
      ),
    )
  );
}

但我不断收到我的 setState 函数未定义的错误。我的主要应用程序是一个 statefullwidget,所以这很奇怪。谁能帮帮我?

这是完整的代码!

import 'package:flutter/material.dart';

final Color backgroundColor = Color(0xFF4A4A58);

class MenuDashboard extends StatefulWidget {
  @override
  _MenuDashboardState createState() => _MenuDashboardState();
}

class _MenuDashboardState extends State<MenuDashboard> {

  bool isCollapsed = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: backgroundColor,
      body: Stack(
        children: <Widget>[
          menu(context),
          dashboard(context),
        ],
      ),
    );
  }
}

Widget menu(context) {
  return Padding(
    padding: const EdgeInsets.only(left: 16.0),
    child: Align(
      alignment: Alignment.centerLeft,
    child: Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
      Text("Home", style: TextStyle(color: Colors.white, fontSize: 20),),
      SizedBox(height: 10.0),
      Text("Brochures", style: TextStyle(color: Colors.white, fontSize: 20),),
        SizedBox(height: 10.0),
      Text("Visitekaartje", style: TextStyle(color: Colors.white, fontSize: 20),),
        SizedBox(height: 10.0),
      ]
     )
    )
  );
}

Widget dashboard(context) {
  return Material(
    elevation: 8,
    color: backgroundColor,
    child: Container(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 48),
      child: Column(
        children: <Widget>[
          Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
            InkWell(child: Icon(Icons.menu, color: Colors.white), onTap: () {
              setState(() {
                isCollapsed = !isCollapsed;
              });
            },),
            Text("Corona App", style: TextStyle(fontSize: 24.0, color: Colors.white)),
                Icon(Icons.settings, color: Colors.white),
          ])
        ],
      ),
    )
  );
}

带有错误消息:'未定义函数 setState'

【问题讨论】:

  • 请发布完整代码。
  • 你的班级里有dashboard()吗?同时发布错误消息。
  • 编辑:我发布了完整的代码
  • 您的仪表板小部件必须在类范围之外..

标签: flutter setstate


【解决方案1】:

setStateState 类的成员,这意味着它只能在扩展StateStatefulWidget 的类中调用。如果你的类是扩展StatelessWidget,你不能调用超类提供的状态成员/方法,包括setStateinitStatedispose方法。

对于答案,您不能在State 类之外调用setState,如果可能,请将小部件方法放在类中,或将setState 函数作为参数传递给这些方法。

【讨论】:

  • 这个类已经是一个 statefulWidget,这就是为什么我觉得它很奇怪
  • @mikagrijpma 但是您没有将这些小部件方法放在类中。这就是错误的原因。
  • 哇,非常感谢你!!这确实是个问题!
【解决方案2】:

setState 只能在StatefulWidget 内部调用。因此,如果我做对了,您可以在 Widget dashboard or menu 小部件中使用 setState。它不能工作,因为那些小部件在课外。尝试将widgets 放入类中

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2020-07-13
    • 2021-01-20
    • 2019-03-30
    相关资源
    最近更新 更多