在飞镖中做一个按钮异步

发布于 2025-02-11 00:38:07 字数 789 浏览 1 评论 0原文

我必须将这两个“点击”功能仅在一个按钮中。问题是,我只能在navigator.push savequestionmodel() and loadQuestions()已经执行。

 onTap: () {
    saveQuestionModel(_key, snapshot.data);
    loadQuestions(_key);
 },

 onTap: () {
    Navigator.push(
     context,
     MaterialPageRoute(
        builder: (context) => QuizScreen(
           code: _key,
           index: _currentIndex,
        ),
     ),
  );

我尝试过这样的尝试,但不起作用,

 onTap: () async {
    saveQuestionModel(_key, snapshot.data);
    loadQuestions(_key);
    await Navigator.push(
       context,
       MaterialPageRoute(
         builder: (context) => QuizScreen(
           code: _key,
           index: _currentIndex,
         ),
      ),
    );
  },

如果不使用两个分开的按钮,有办法这样做吗?

I have to put this two "on tap" functions in only one button. The thing is that I only can execute the Navigator.push after saveQuestionModel() and loadQuestions() have been already executed.

 onTap: () {
    saveQuestionModel(_key, snapshot.data);
    loadQuestions(_key);
 },

 onTap: () {
    Navigator.push(
     context,
     MaterialPageRoute(
        builder: (context) => QuizScreen(
           code: _key,
           index: _currentIndex,
        ),
     ),
  );

I tried like this but did not work

 onTap: () async {
    saveQuestionModel(_key, snapshot.data);
    loadQuestions(_key);
    await Navigator.push(
       context,
       MaterialPageRoute(
         builder: (context) => QuizScreen(
           code: _key,
           index: _currentIndex,
         ),
      ),
    );
  },

Is there a way of doing that without using two separated buttons?

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

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

发布评论

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

评论(2

山田美奈子 2025-02-18 00:38:07

创建一个单独的方法,该方法将保存您的所有方法并在将来的方法之前使用等待

void myFun() async{
   
   await myFutureLoad();   
   // include others
   ....

}

并在widget ontap 上使用此myfun

onTap: myFun,

,您可以为

 onTap: () async{
    await saveQuestionModel(_key, snapshot.data);
    await loadQuestions(_key);
     Navigator.push(
     context,
     MaterialPageRoute(
        builder: (context) => QuizScreen(
           code: _key,
           index: _currentIndex,
        ),
     ),
  );
 },

dart async-wait

Create a separate method that will hold all your methods and use await before future methods.

void myFun() async{
   
   await myFutureLoad();   
   // include others
   ....

}

And use this myFun on widget onTap

onTap: myFun,

For your case, you can do

 onTap: () async{
    await saveQuestionModel(_key, snapshot.data);
    await loadQuestions(_key);
     Navigator.push(
     context,
     MaterialPageRoute(
        builder: (context) => QuizScreen(
           code: _key,
           index: _currentIndex,
        ),
     ),
  );
 },

More about dart async-await

双手揣兜 2025-02-18 00:38:07

在您的情况下,它

onTap:()async{
  await saveQuestionModel();
  await loadQuestions();
  
  Navigator.push(...)
}

基本上应该在这里等待两个函数结束执行,然后再使用navigator.push()来推动下一页。

In your case, it should be

onTap:()async{
  await saveQuestionModel();
  await loadQuestions();
  
  Navigator.push(...)
}

Here basically it waits for both the functions to end executing before pushing the next page using Navigator.push()

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