ValueListenableBuilder 处理 Map

发布于 2025-01-16 15:39:00 字数 515 浏览 1 评论 0原文

我想从数据声明之外的小部件修改数据 (Map)。例如:

  class Home extends StatefulWidget {
    //...some code
    Map<int,String> myMap = {0:"Hi"};
    //...rest of the class

Home 的新子页面:

class Menu extends StatefulWidget {
//...some code
myMap.clear();
//...rest of the class

我该怎么做?我正在阅读有关 ValueListenableBuilder 的内容,这对我有帮助吗?显然,在 clear() 调用后,Home 内的 Map 应该为空。

I want to modify a data (Map<int,String>)from a widget outside my data declaration. For example :

  class Home extends StatefulWidget {
    //...some code
    Map<int,String> myMap = {0:"Hi"};
    //...rest of the class

A new page child of Home :

class Menu extends StatefulWidget {
//...some code
myMap.clear();
//...rest of the class

How can I do this? I was reading something about ValueListenableBuilder can this help me? Obviously Map inside Home should be empty after clear() call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2025-01-23 15:39:00

您可以在此处使用功能检查。



class HomePage extends StatelessWidget {
   HomePage();

  Map<int,String> myMap = {0:"Hi"};
  
  void onCalledFromOutside() {
    print("befor clear value");
    print(myMap);
    
    myMap.clear();
      print("after clear value");
    print(myMap);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
    child: ElevatedButton(
          onPressed: () { Navigator.of(context).push(
             MaterialPageRoute(
               builder: (context) => AnotherPage(onCalledFromOutside),
             ));
                        },
          child: const Text("Go to AnotherPage"),
        ),
      ),
    );
  }
}

class AnotherPage extends StatelessWidget {
  final Function callback;

  const AnotherPage(this.callback);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
    child: ElevatedButton(
           onPressed: callback as VoidCallback,
           child:const Text("Press me to clear Map"),
        ),
      ),
    );
  }
}

you can do using function check here.



class HomePage extends StatelessWidget {
   HomePage();

  Map<int,String> myMap = {0:"Hi"};
  
  void onCalledFromOutside() {
    print("befor clear value");
    print(myMap);
    
    myMap.clear();
      print("after clear value");
    print(myMap);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
    child: ElevatedButton(
          onPressed: () { Navigator.of(context).push(
             MaterialPageRoute(
               builder: (context) => AnotherPage(onCalledFromOutside),
             ));
                        },
          child: const Text("Go to AnotherPage"),
        ),
      ),
    );
  }
}

class AnotherPage extends StatelessWidget {
  final Function callback;

  const AnotherPage(this.callback);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
    child: ElevatedButton(
           onPressed: callback as VoidCallback,
           child:const Text("Press me to clear Map"),
        ),
      ),
    );
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文