Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 未找到方法:';设置状态';即使我';我在一个有状态的小部件中_Flutter_Dart - Fatal编程技术网

Flutter 未找到方法:';设置状态';即使我';我在一个有状态的小部件中

Flutter 未找到方法:';设置状态';即使我';我在一个有状态的小部件中,flutter,dart,Flutter,Dart,我有个问题。在过去的几天里,我一直在和弗利特玩。但现在我撞到了墙。我的问题是,我在有状态小部件中的列表中。当我编译应用程序时,它会说“methodnotfound:‘setState’”,如果我在一个无状态的小部件中,这是有意义的,但我在一个有状态的小部件中,所以它应该可以工作 这是我的完整代码: import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flu

我有个问题。在过去的几天里,我一直在和弗利特玩。但现在我撞到了墙。我的问题是,我在有状态小部件中的列表中。当我编译应用程序时,它会说“methodnotfound:‘setState’”,如果我在一个无状态的小部件中,这是有意义的,但我在一个有状态的小部件中,所以它应该可以工作

这是我的完整代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


void main() {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(statusBarColor: Colors.transparent));
  runApp(MyApp());
}

double _value = 25.0;
bool _switchValue = false;

class MyApp extends StatefulWidget {
  const MyApp();

  @override
  Slider createState() => Slider();
}

class Slider extends State<MyApp> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
  TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  List<Widget> _widgetOptions = <Widget>[
    Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        SizedBox(
          //width: (double.infinity),

            child: CupertinoSlider(
              value: _value,
              min: 0.0,
              max: 100.0,
              onChanged: (value){
                setState((){
                  _value = value;

                });
              },
            )
        ),
        MergeSemantics(
            child: CupertinoSwitch(
                value: _switchValue,
                onChanged: (value) {
                  setState(() {
                    _switchValue = value;
                  });
                }
            )
        )
      ],
    ),
    Text(
      'Index 1: Business',
      style: optionStyle,
    ),
  ];

  IconData icon_calender_state = CupertinoIcons.calendar_badge_plus;

  void _onItemTapped() {
  setState(() {
  if (_selectedIndex == 0){
    _selectedIndex = 1;
    icon_calender_state = CupertinoIcons.calendar_badge_minus;
    return;
  }
  if (_selectedIndex == 1){
    _selectedIndex = 0;
    icon_calender_state = CupertinoIcons.calendar_badge_plus;
    return;
  }

  });
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Welcome to Flutter',
        home: Scaffold(

            body: Stack(
            children: <Widget>[
              Center(
              child: Container(
              decoration: BoxDecoration(
              gradient: LinearGradient(
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [Colors.blue, Colors.red])),
                child: Center(
                  child: _widgetOptions.elementAt(_selectedIndex),
              ))),
              Align(
              alignment: Alignment.bottomLeft,
                child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  IconButton(icon: Icon(icon_calender_state), onPressed: () {
                    _onItemTapped();
                  },),
    ],)),

    ],
    )));}
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
void main(){
SystemChrome.setSystemUserIOVerlayStyle(SystemUiOverlayStyle(statusBarColor:Colors.transparent));
runApp(MyApp());
}
双_值=25.0;
bool _switchValue=false;
类MyApp扩展了StatefulWidget{
常量MyApp();
@凌驾
Slider createState()=>Slider();
}
类滑块扩展状态{
int _selectedIndex=0;
静态常量文本样式选项样式=
TextStyle(fontSize:30,fontWeight:fontWeight.bold);
列表_widgetOptions=[
纵队(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
大小盒子(
//宽度:(双无限),
孩子:铜滑环(
值:_值,
最小值:0.0,
最高:100.0,
一旦更改:(值){
设置状态(){
_价值=价值;
});
},
)
),
合并语义学(
孩子:CupertinoSwitch(
值:_开关值,
一旦更改:(值){
设置状态(){
_开关值=开关值;
});
}
)
)
],
),
正文(
"指数1:业务",,
样式:可选样式,
),
];
IconData icon_calendar_state=CupertinoIcons.calendar_badge_plus;
void _onItemTapped(){
设置状态(){
如果(_selectedIndex==0){
_selectedIndex=1;
图标\日历\状态=CupertinoIcons.calendar \徽章\负号;
返回;
}
如果(_selectedIndex==1){
_selectedIndex=0;
icon_calendar_state=CupertinoIcons.calendar_badge_plus;
返回;
}
});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
标题:“欢迎来到弗利特”,
家:脚手架(
主体:堆栈(
儿童:[
居中(
子:容器(
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topRight,
结束:对齐。左下角,
颜色:[颜色。蓝色,颜色。红色]),
儿童:中心(
子项:_widgetOptions.elementAt(_selectedIndex),
))),
对齐(
对齐:对齐。左下角,
子:列(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
图标按钮(图标:图标(图标日历状态),ON按下:(){
_未标记();
},),
],)),
],
)));}
}
这只是错误所在的部分。下面列表中每次调用setState都会返回上述错误

class Slider extends State<MyApp> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
  TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  List<Widget> _widgetOptions = <Widget>[
    Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        SizedBox(
          //width: (double.infinity),

            child: CupertinoSlider(
              value: _value,
              min: 0.0,
              max: 100.0,
              onChanged: (value){
                setState((){
                  _value = value;

                });
              },
            )
        ),
        MergeSemantics(
            child: CupertinoSwitch(
                value: _switchValue,
                onChanged: (value) {
                  setState(() {
                    _switchValue = value;
                  });
                }
            )
        )
      ],
    ),
    Text(
      'Index 1: Business',
      style: optionStyle,
    ),
  ];

类滑块扩展状态{
int _selectedIndex=0;
静态常量文本样式选项样式=
TextStyle(fontSize:30,fontWeight:fontWeight.bold);
列表_widgetOptions=[
纵队(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
大小盒子(
//宽度:(双无限),
孩子:铜滑环(
值:_值,
最小值:0.0,
最高:100.0,
一旦更改:(值){
设置状态(){
_价值=价值;
});
},
)
),
合并语义学(
孩子:CupertinoSwitch(
值:_开关值,
一旦更改:(值){
设置状态(){
_开关值=开关值;
});
}
)
)
],
),
正文(
"指数1:业务",,
样式:可选样式,
),
];

有人知道为什么会发生这种情况吗?有人建议我应该如何更改代码吗?

您正在遵循一些关于颤振和将小部件存储在变量中的糟糕做法。这通常是一个很大的禁忌,因为小部件是不可变的,不会按照您的预期进行更新

build
中声明
List
,或者将其更改为返回
List
的函数,而不是在
build
中调用的函数

List\u widgetOptions(){
返回[
纵队(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
大小盒子(
//宽度:(双无限),
孩子:铜滑环(
值:_值,
最小值:0.0,
最高:100.0,
一旦更改:(值){
设置状态(){
_价值=价值;
});
},
)
),
合并语义学(
孩子:CupertinoSwitch(
值:_开关值,
一旦更改:(值){
设置状态(){
_开关值=开关值;
});
}
)
)
],
),
正文(
"指数1:业务",,
样式:可选样式,
),
];
}

Christopher的回答告诉您如何解决问题,但问题实际上是您在
\u widgetOptions
类字段的初始值设定项中调用了
setState
。您不能在该类中未标记为
static
或其他ac的初始值设定项中使用该类的任何成员可从静态上下文访问(即不显式或隐式调用
this