flutter - 推送返回空值

标签 flutter async-await null navigator issuu

当我点击按钮时 该值是 Navigator.push() 方法的返回值。 “因为按下返回 null 的 Button,不会使 Future 变为 null,而是使其内容变为 null。这会导致无意的行为”

enter image description here 主屏幕

PopupMenuButton(
            icon: Icon(Icons.more_vert,color: Colors.black,),
            offset: Offset(0, 40),
            itemBuilder: (context) => [
              PopupMenuItem(
                child: Text('View Cart'),
                onTap: () async {
                final result = await Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => CartScreen(cart: cart)),
                  ) ;
                  if(result)
                  {
                    setState(() {
                      
                    });
                  }
                },
              ),

购物车屏幕

 WillPopScope(

      onWillPop: () {
        Navigator.pop(context,true);

         return new Future(() => false);;
      },
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          iconTheme: IconThemeData(color: Colors.black),
          elevation: 0,
        ),

当我单击按钮导航页面时

最佳答案

我有这样的例子:

主页

  await Navigator.of(context).pushNamed(
        PageNames.routeTest,
      ).then(
        (result) {
            if (result != null && result as bool) {
                [...do something...]
            }
          }
        },
      );

测试页

可能只是一个

/// Could be just like :
Navigator.of(context).pop(true);

/// Or
 onWillPop: () {
    Navigator.of(context).pop(true);
    return true;
 },

尝试在 future 函数中使用 .then 并使用 Navigator.of(context).pop。告诉我这是否有效

关于flutter - 推送返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77314859/

相关文章:

flutter - 无法更改下拉按钮值 Flutter

dart - Nested Future 中的导航

flutter - 在Flutter中使用Bloc/Cubit时避开CONTEXT

c# - 任务不会被垃圾收集

c# - 异步等待执行顺序 - 代码仅在单步调试/调试时才真正起作用

c# - ITargetBlock<TInput>.Completion.ContinueWith() 的最佳实践

php - 通过 __get() 通过引用返回 null

sql - 使用空列创建唯一约束

flutter - 将数据库提供程序导入另一个文件提供程序 Dart

php - 在 php 中创建具有空值的 XML 节点