如何在颤振中做到这一点?数据传递|函数调用|导航



假设我的代码当前在类x的一个函数中。在这个函数中,我想使用Navigator。pushNamed到另一个类B,然后在结果填充后,我想回到类X并执行其余的代码。

Class X {
function a() {
--- Do something --
// Go to class B and perform a specific operation - Don't move forward until class B is popped and we have the required result
// Come back with the result of operation which we did in previous step
-- Perform rest of the function calls using the result of operation --
}
}

像这样的东西可能使用flutter吗?目前,我的代码确实将我路由到另一个页面(类B),但在类B完成填充结果之前,考虑到调用是异步的,其余的代码已经执行。

此外,我不确定如何将数据从类B发送回类X,然后才继续使用函数的其余部分

Navigater有一个。then方法所以你可以

Navigator.of(context).push(nextscreen).then((res)=>{
// res will contain the data you passed back
// Rest of your code here
});

和下一屏

Navigator.pop(variable); // if you have data to pass back

要在弹出小部件'ClassB'后执行代码,

  • 等待ClassB弹出时的结果

var =等待Navigator.push(……结果//这里按到B

  • 在B中,弹出它,返回结果

导航器。pop(上下文,someResult)

  • 使函数a、异步

void a() async {.....

因为async-await关键字,你的代码(在你推到ClassB之后的行)只会在弹出ClassB之后执行

最新更新