简体   繁体   English

从 Flutter 中的另一个 dart 文件在有状态小部件 class 下使用 SetState 方法调用 void

[英]Call void with SetState method under stateful widget class from another dart file in Flutter

I have one class containing stateful widget called _DispState with ListView.builder which shows some text downloaded from server:我有一个 class 包含名为 _DispState 和 ListView.builder 的有状态小部件,它显示了从服务器下载的一些文本:

class Disp extends StatefulWidget {
  Disp({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _DispState createState() => _DispState();
}


class _DispState extends State<Disp> {

void refresher() async {
    while (true) {
      update();  //void function which downloads data from server and stores them into global variable "var AllData"
      setState(() {
        zobrazitPracu(); //splits and separate data from variable AllData into "List mojaPraca"
      });
      await Future.delayed(Duration(milliseconds: 3000));
    }
  }

  void oneTimeRefresh() async {
    update();
    setState(() {
      zobrazitPracu();
    });
  }

@override
  void initState() {
    super.initState();
    refresher(); //starts refresher to update data from server every 3 seconds
  }

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Disp'),
      ),
      body: Center(
        child: ListView.builder(
            //very long code to show items one by one from "List mojaPraca"
        )
      )
}

There is refresher() function which updates list every 3 seconds.有 refresher() function 每 3 秒更新一次列表。 But I have another stateful widget in another dart file called _mavacka which creates new items and sends them to server on button click.但是我在另一个名为 _mavacka 的 dart 文件中有另一个有状态小部件,它创建新项目并在单击按钮时将它们发送到服务器。 I would like to achieve updating list in _DispState right after sending new item to server.我想在将新项目发送到服务器后立即在 _DispState 中实现更新列表。 I can't just put that data into mojaPraca List because at first it need to get ID generated by server.我不能只将这些数据放入 mojaPraca 列表,因为首先它需要获取服务器生成的 ID。 I have function oneTimeRefresh() to do it but i can't find the way how to call it from button press event in _mavacka state(which is in another dart file).我有 function oneTimeRefresh() 来执行此操作,但我找不到如何从 _mavacka 状态下的按钮按下事件中调用它的方法(在另一个 dart 文件中)。 I have heard about some keys and voidcallbacks but i haven't find them helpful for me and even maybe didn't understand them well (if is one of these 2 ways correct way how to do that, please try to explain this to me step by step).我听说过一些键和 voidcallbacks,但我没有发现它们对我有帮助,甚至可能不太了解它们(如果这两种方法之一是正确的方法,请尝试向我解释这一步一步一步)。 I don't want to pass any data, just call that one function.我不想传递任何数据,只需调用一个 function。 I am new to flutter programming, so maybe i am doing it completely wrong.我是 flutter 编程的新手,所以也许我做错了。 If so, please correct me.如果是这样,请纠正我。 If not, please help me find out the way how to call oneTimeRefresh() by clicking on button in another state.如果没有,请帮助我了解如何通过单击另一个 state 中的按钮来调用 oneTimeRefresh()。 If there is other, mmore efficient way how to update List mojaPraca and then setState in _DispState please tell me.如果有其他更有效的方法如何更新 List mojaPraca 然后 setState in _DispState 请告诉我。

Thanks a lot:)非常感谢:)

I have one class containing stateful widget called _DispState with ListView.builder which shows some text downloaded from server:我有一个 class 包含名为 _DispState 和 ListView.builder 的有状态小部件,它显示了从服务器下载的一些文本:

class Disp extends StatefulWidget {
  Disp({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _DispState createState() => _DispState();
}


class _DispState extends State<Disp> {

void refresher() async {
    while (true) {
      update();  //void function which downloads data from server and stores them into global variable "var AllData"
      setState(() {
        zobrazitPracu(); //splits and separate data from variable AllData into "List mojaPraca"
      });
      await Future.delayed(Duration(milliseconds: 3000));
    }
  }

  void oneTimeRefresh() async {
    update();
    setState(() {
      zobrazitPracu();
    });
  }

@override
  void initState() {
    super.initState();
    refresher(); //starts refresher to update data from server every 3 seconds
  }

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Disp'),
      ),
      body: Center(
        child: ListView.builder(
            //very long code to show items one by one from "List mojaPraca"
        )
      )
}

There is refresher() function which updates list every 3 seconds.有 refresher() function 每 3 秒更新一次列表。 But I have another stateful widget in another dart file called _mavacka which creates new items and sends them to server on button click.但是我在另一个名为 _mavacka 的 dart 文件中有另一个有状态小部件,它创建新项目并在单击按钮时将它们发送到服务器。 I would like to achieve updating list in _DispState right after sending new item to server.我想在将新项目发送到服务器后立即在 _DispState 中实现更新列表。 I can't just put that data into mojaPraca List because at first it need to get ID generated by server.我不能只将这些数据放入 mojaPraca 列表,因为首先它需要获取服务器生成的 ID。 I have function oneTimeRefresh() to do it but i can't find the way how to call it from button press event in _mavacka state(which is in another dart file).我有 function oneTimeRefresh() 来执行此操作,但我找不到如何从 _mavacka 状态下的按钮按下事件中调用它的方法(在另一个 dart 文件中)。 I have heard about some keys and voidcallbacks but i haven't find them helpful for me and even maybe didn't understand them well (if is one of these 2 ways correct way how to do that, please try to explain this to me step by step).我听说过一些键和 voidcallbacks,但我没有发现它们对我有帮助,甚至可能不太了解它们(如果这两种方法之一是正确的方法,请尝试向我解释这一步一步一步)。 I don't want to pass any data, just call that one function.我不想传递任何数据,只需调用一个 function。 I am new to flutter programming, so maybe i am doing it completely wrong.我是 flutter 编程的新手,所以也许我做错了。 If so, please correct me.如果是这样,请纠正我。 If not, please help me find out the way how to call oneTimeRefresh() by clicking on button in another state.如果没有,请帮助我了解如何通过单击另一个 state 中的按钮来调用 oneTimeRefresh()。 If there is other, mmore efficient way how to update List mojaPraca and then setState in _DispState please tell me.如果有其他更有效的方法如何更新 List mojaPraca 然后 setState in _DispState 请告诉我。

Thanks a lot:)非常感谢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM