为什么我的setState在弹出屏幕后调用函数?

huangapple go评论74阅读模式
英文:

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() =&gt; _HomeScreenState();
}

class _HomeScreenState extends State&lt;HomeScreen&gt; {

  void set() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: &lt;Widget&gt;[
          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: (_) =&gt; 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()

huangapple
  • 本文由 发表于 2023年6月29日 04:25:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定