英文:
Why is my setState in function call after popping out of screen?
问题
所以,我试图在从另一个导航器屏幕弹出时设置我的小部件的状态,但出于某种原因它就是不起作用。
这是我的代码...
class HomeScreen extends StatefulWidget {
const HomeScreen({Key key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
void set() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: IconButton(
color: Colors.white,
iconSize: 30,
icon: const Icon(Icons.notifications_active_outlined),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const NextScreen(),
),
).then((value) {
set();
});
},
),
),
],
),
);
}
}
如果我直接将setState(() {});
放在.then((value) { setState(() {});});
中,setState
将起作用,但我需要一个函数,因为我计划从其他一些屏幕设置状态,传递一个函数将是最好的方法。
任何帮助都将不胜感激!
英文:
So I'm trying to setState of my widget when popping out of another navigator screen and for some reason it just won't work.
Here's my code..
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
void set() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: IconButton(
color: Colors.white,
iconSize: 30,
icon: const Icon(Icons.notifications_active_outlined),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const NextScreen()))
.then((value) {
set;
});
}),
],
),),
}
The set state works if I put it directly in as.then((value) { setState(() {});});
but I need a function as I'm planning on setting the states from some other screens as well and passing a function would be the best way to do so.
Any help is appreciated!
答案1
得分: 4
你正在错误地调用你的设置函数
应该是
set()
英文:
you are calling your set function incorrectly
should be
set()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论