Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Listview 为什么异步等待在这种情况下不起作用?_Listview_Flutter_Dart_Async Await - Fatal编程技术网

Listview 为什么异步等待在这种情况下不起作用?

Listview 为什么异步等待在这种情况下不起作用?,listview,flutter,dart,async-await,Listview,Flutter,Dart,Async Await,为什么async wait在我的情况下不起作用?我在pageA有一个按钮。点击后,弹出删除确认对话框 单击确认对话框中的Yes按钮后,它将从服务器中删除数据并返回一个成功代码,即返回到PageA 一旦pageA收到返回值,它将刷新列表视图 PageA按钮 onTap: () async { Navigator.pop(context); var result = await PopUpDialog().showDeleteDialog(); if (result == 101) {

为什么
async wait
在我的情况下不起作用?我在pageA有一个按钮。点击后,弹出删除确认对话框

单击确认对话框中的
Yes
按钮后,它将从服务器中删除数据并返回一个成功代码,即返回到PageA

一旦pageA收到返回值,它将刷新
列表视图

PageA按钮

onTap: () async {
  Navigator.pop(context);
  var result = await PopUpDialog().showDeleteDialog();
  if (result == 101) {
    setState(() {
      data.removeAt(index);   // remove the index in listView
    });
  } else {
    print('fjeodpedp');
  }
},
弹出对话框

class PopUpDialog {
  var result;
  Future<int> showDeleteDialog() async {
         ....
    showDialog(
        context: context,
        builder: (BuildContext buildContext) {
          return AlertDialog(
              actions: <Widget>[
                FlatButton(
                  color: Colors.orange,
                  child: Text('YES', style: TextStyle(color: Colors.white)),
                  onPressed: () async {
                    Navigator.pop(buildContext);
                    result = await _bloc.delete();   // delete server data
                    if (result == 101) {
                      return result;
                    }
                  },
                ),
                FlatButton(
                  color: Colors.white,
                  child: Text('CANCEL'),
                  onPressed: () {
                    Navigator.of(buildContext, rootNavigator: true)
                        .pop('dialog');
                  },
                )
              ],
              ....
        });

    return 111; 
  }
}
类弹出对话框{
var结果;
Future showDeleteDialog()异步{
....
显示对话框(
上下文:上下文,
生成器:(BuildContext BuildContext){
返回警报对话框(
行动:[
扁平按钮(
颜色:颜色。橙色,
子项:文本('YES',样式:TextStyle(颜色:Colors.white)),
onPressed:()异步{
pop(buildContext);
结果=等待_bloc.delete();//删除服务器数据
如果(结果=101){
返回结果;
}
},
),
扁平按钮(
颜色:颜色,白色,
子项:文本('CANCEL'),
已按下:(){
of(buildContext,rootNavigator:true)
.pop('dialog');
},
)
],
....
});
返回111;
}
}

但问题是,一旦删除确认对话框弹出,我将得到fjeodpedp

因为
showDialog
是异步的,所以在对话框完成之前,您
返回111


您可以尝试使用
setState((){})
来代替它,这样它就不会变得太乱了

因为
showDialog
是异步的,所以在对话框完成之前,
返回111


您可以尝试使用
setState((){})
来代替它,这样它就不会变得太混乱了在显示对话框后,您马上返回111。不确定您的意图是什么,但在我看来,您不需要这一行,因为您正在处理A页上的返回值


尝试删除这一行,看看它是如何运行的。

在显示对话框后立即返回111。不确定您的意图是什么,但在我看来,您不需要这一行,因为您正在处理A页上的返回值


试着删除这一行,看看它是如何运行的。

我认为如果您按照以下方式操作会更好

Future<int> showDeleteDialog() async {
 final response = showDialog(
    context: context,
    builder: (BuildContext buildContext) {
      return AlertDialog(
          actions: <Widget>[
            FlatButton(
              color: Colors.orange,
              child: Text('YES', style: TextStyle(color: Colors.white)),
              onPressed: () async {
                Navigator.pop(buildContext,'yes');
              },
            ),
            FlatButton(
              color: Colors.white,
              child: Text('CANCEL'),
              onPressed: () {
                Navigator.pop(buildContext,'cancel');
              },
            )
          ]
      );
    }
);

  if(await response == 'yes'){
    return 101;
  }
  return 111;}
Future showDeleteDialog()异步{
最终响应=显示对话框(
上下文:上下文,
生成器:(BuildContext BuildContext){
返回警报对话框(
行动:[
扁平按钮(
颜色:颜色。橙色,
子项:文本('YES',样式:TextStyle(颜色:Colors.white)),
onPressed:()异步{
pop(buildContext,'yes');
},
),
扁平按钮(
颜色:颜色,白色,
子项:文本('CANCEL'),
已按下:(){
pop(buildContext,'cancel');
},
)
]
);
}
);
如果(等待响应=‘是’){
返回101;
}
返回111;}

我想如果你按照下面的方法做会更好

Future<int> showDeleteDialog() async {
 final response = showDialog(
    context: context,
    builder: (BuildContext buildContext) {
      return AlertDialog(
          actions: <Widget>[
            FlatButton(
              color: Colors.orange,
              child: Text('YES', style: TextStyle(color: Colors.white)),
              onPressed: () async {
                Navigator.pop(buildContext,'yes');
              },
            ),
            FlatButton(
              color: Colors.white,
              child: Text('CANCEL'),
              onPressed: () {
                Navigator.pop(buildContext,'cancel');
              },
            )
          ]
      );
    }
);

  if(await response == 'yes'){
    return 101;
  }
  return 111;}
Future showDeleteDialog()异步{
最终响应=显示对话框(
上下文:上下文,
生成器:(BuildContext BuildContext){
返回警报对话框(
行动:[
扁平按钮(
颜色:颜色。橙色,
子项:文本('YES',样式:TextStyle(颜色:Colors.white)),
onPressed:()异步{
pop(buildContext,'yes');
},
),
扁平按钮(
颜色:颜色,白色,
子项:文本('CANCEL'),
已按下:(){
pop(buildContext,'cancel');
},
)
]
);
}
);
如果(等待响应=‘是’){
返回101;
}
返回111;}

我通常将函数参数传递给showDialog函数,如onYes和onCancel,然后从对话框中的onTap或onPressed事件调用这些参数

以下是基于原始代码的功能示例:

import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {
  @override
  createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  final int index = 5; // Sample index
  final data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Sample data

  @override
  Widget build(BuildContext context) {  
    return Container(
      child: RaisedButton(
        child: Text("Click me"),
        onPressed: () {
          showDeleteDialog(
            onYes: () { // This will be called when user taps 'YES'
              setState(() {
                data.removeAt(index); // remove the index in listView
                print("removed");
              });
            },
            onCancel: () { // This will be called when user taps 'CANCEL'
              print('fjeodpedp');
            }
          );
        },
      ),
    );
  }

  void showDeleteDialog({Function onYes, Function onCancel}) async {
    showDialog(
      context: context,
      builder: (BuildContext buildContext) {
        return AlertDialog(
          actions: <Widget>[
            FlatButton(
              color: Colors.orange,
              child: Text('YES', style: TextStyle(color: Colors.white)),
              onPressed: () async {
                Navigator.pop(buildContext);
                if (null != onYes) onYes(); // Ensure the reference exists before calling it
              },
            ),
            FlatButton(
              color: Colors.white,
              child: Text('CANCEL'),
              onPressed: () {
                Navigator.pop(buildContext);
                if (null != onCancel) onCancel(); // Ensure the reference exists before calling it
              },
            )
          ],
        );
      }
    );
  }
}
导入“包装:颤振/材料.省道”;
类TestPage扩展了StatefulWidget{
@凌驾
createState()=>_TestPageState();
}
类_TestPageState扩展了状态{
final int index=5;//示例索引
最终数据=[1,2,3,4,5,6,7,8,9,10];//示例数据
@凌驾
小部件生成(BuildContext上下文){
返回容器(
孩子:升起按钮(
子:文本(“单击我”),
已按下:(){
显示删除对话框(
onYes:(){//当用户点击“是”时将调用此选项
设置状态(){
data.removeAt(index);//删除listView中的索引
打印(“删除”);
});
},
onCancel:(){//当用户点击“取消”时,将调用此选项
打印(“fjeodpedp”);
}
);
},
),
);
}
void showDeleteDialog({Function onYes,Function onCancel})异步{
显示对话框(
上下文:上下文,
生成器:(BuildContext BuildContext){
返回警报对话框(
行动:[
扁平按钮(
颜色:颜色。橙色,
子项:文本('YES',样式:TextStyle(颜色:Colors.white)),
onPressed:()异步{
pop(buildContext);
if(null!=onYes)onYes();//在调用之前确保引用存在
},
),
扁平按钮(
颜色:颜色,白色,
子项:文本('CANCEL'),
已按下:(){
Navigator.pop(buildConte