gpt4 book ai didi

dart - async.Future async.Completer-如果发生错误,如何 “continue”

转载 作者:行者123 更新时间:2023-12-03 02:51:54 24 4
gpt4 key购买 nike

在下面的一些帮助将不胜感激。我正在编写一些控制台测试程序,并且希望能够从终端输入一些参数(我不想使用命令行参数-参数太多)。我已经尝试了一些变体,但是找不到如何完成此操作。以下是我的终端输入测试的最新版本。该程序的问题在于,如果遇到错误,完成程序将自动关闭,并且我想从Main()或fGetNumber()函数继续。虽然我可以看到为什么该程序无法正常工作,但它说明了我需要实现的目标-重新输入数字,但我找不到实现该目标的方法。如果输入有效数字,则没有问题。如果输入了无效的号码,我将找不到如何重新输入该号码的方法。

代码如下,并且我遇到的问题以“//////////”突出显示:

import "dart:async" as async;
import "dart:io";

void main() {
fGetNumber("Enter Nr of Iterations : ", 0, 999999)
.then((int iIters){
print ("In Main : Iterations selected = ${iIters}");
if (iIters == null) {
print ("In Main: Invalid Number of iterations : ${iIters}.");
} else {
fProcessData(iIters);
}
print ("Main Completed");
});
}

async.Future<int> fGetNumber(String sPrompt, int iMin, int iMax) {
print ("In fGetNumber");
int iIters = 0;
async.Completer<int> oCompleter = new async.Completer();

while (!oCompleter.isCompleted) { /////////// This loop does not work ///////
return fGetUserInput(sPrompt).then((String sIters) {
iIters = int.parse(sIters);
if (iIters < iMin || iIters > iMax) throw new Exception("Invalid");
oCompleter.complete(iIters);
return oCompleter.future;
}).catchError((_) => print ("Invalid - number must be from ${iMin} to ${iMax}")
).whenComplete(() => print ("fGetNumber - whenComplete"));// always gets here
}
print ("In fGetNumber (at end of function)"); //// it never gets here
}

async.Future<String> fGetUserInput(String sPrompt) {
print ("In fGetUserInput");
async.Completer<String> oCompleter = new async.Completer();

stdout.write(sPrompt);
async.Stream<String> oStream = stdin.transform(new StringDecoder());
async.StreamSubscription oSub;
oSub = oStream.listen((String sData) {
oCompleter.complete("$sData");
oSub.cancel();
});

return oCompleter.future;
}

void fProcessData(int iIters) {
print ("In fProcessData");
for (int iPos = 1; iPos <= iIters; iPos++ ) {
if (iPos%100 == 0) print ("Processed = ${iPos}");
}
print ("In fProcessData - completed ${iIters}");
}

最佳答案

// This loop does not work



当然可以,只需输入一次即可,然后立即返回并退出循环和方法。

// always gets here



这是因为 whenComplete()总是在成功或错误时被调用。

// it never gets here



因为您已经退出了该方法。

那么该怎么办?

最简单的方法是不依赖 fGetUserInput()。收听 stdin中的 fGetNumber,仅在输入有效的情况下完成完成者/取消订阅:

async.Future<int> fGetNumber(String sPrompt, int iMin, int iMax) {

print ("In fGetNumber");
async.Completer<String> oCompleter = new async.Completer();

stdout.write(sPrompt);
async.Stream<String> oStream = stdin.transform(new StringDecoder());
async.StreamSubscription oSub;
oSub = oStream.listen((String sData) {
try {
int iIters = int.parse(sData);
if (iIters < iMin || iIters > iMax) throw new Exception("Invalid");
oCompleter.complete(iIters);
oSub.cancel();
} catch(e) {
print("Invalid - number must be from ${iMin} to ${iMax}");
stdout.write(sPrompt);
}
});

return oCompleter.future;
}

还有其他选择吗?

当然。可能有很多很多方法可以做到这一点。以这个为例:

async.Future<int> fGetNumber(String sPrompt, int iMin, int iMax) {
print ("In fGetNumber");
async.Completer<int> oCompleter = new async.Completer();
fGetUserInput(sPrompt, oCompleter, (String sIters) {
try {
int iIters = int.parse(sIters);
if (iIters < iMin || iIters > iMax) throw new Exception("Invalid");
return iIters;
} catch(e) {
print ("Invalid - number must be from ${iMin} to ${iMax}");
stdout.write(sPrompt);
}
return null;
});
return oCompleter.future;
}

void fGetUserInput(String sPrompt, async.Completer oCompleter, dynamic inputValidator(String sData)) {
print ("In fGetUserInput");
stdout.write(sPrompt);
async.Stream<String> oStream = stdin.transform(new StringDecoder());
async.StreamSubscription oSub;
oSub = oStream.listen((String sData) {
var d = inputValidator(sData);
if(d != null) {
oCompleter.complete(d);
oSub.cancel();
}
});
}

如果您真的觉得Dart团队应该解决一些问题,则可以编写 feature request。但是 Completer只能完成一次。无论您编写什么代码,都无法循环反复完成它。

关于dart - async.Future async.Completer-如果发生错误,如何 “continue”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com