Dart 服务器回调发生4次,而不是1次
我注意到在执行我的服务器调用时发生了以下情况,这里出了什么问题Dart 服务器回调发生4次,而不是1次,dart,dart-html,dart-async,Dart,Dart Html,Dart Async,我注意到在执行我的服务器调用时发生了以下情况,这里出了什么问题 getItems(){ 打印('getItems'); 请求=新的HttpRequest(); request.onReadyStateChange.listen(onData_getItems); 打开('POST',host+'/getItems'); 请求发送(“”); } onData_获取项目(u){ 打印('call');//-->打印4次!! 如果(request.readyState==HttpRequest.DON
getItems(){
打印('getItems');
请求=新的HttpRequest();
request.onReadyStateChange.listen(onData_getItems);
打开('POST',host+'/getItems');
请求发送(“”);
}
onData_获取项目(u){
打印('call');//-->打印4次!!
如果(request.readyState==HttpRequest.DONE&&request.status==200){
打印('数据加载成功..';
打印(request.responseText);//-->打印一次!!
for(JSON.decode(request.responseText)中的映射项){
LineItem..children.add(新的OptionElement(值:item['Code'],数据:item['Name']);
}
}
else if(request.readyState==HttpRequest.DONE&&
request.status==0){
打印(“无法连接到服务器,请与管理员联系”);
}
else打印(“其他东西”);//-->打印3次
}
以上内容在我的自定义元素中执行:
类getPurchaseLines扩展了HtmleElement{
静态final标记='get POlines';
factory getPurchaseLines()=>新元素.tag(tag);
var影子、行项目、行数量、行价格、clsBtn;
getPurchaseLines.created():super.created(){
shadow=this.createShadowRoot();
LineItem=new SelectElement()
…子项。添加(新的OptionElement(值:“0”,数据:“股票代码”);
LineQty=new InputElement()…type='number'…占位符='Qty'…style.width='50px';
Lineprice=newInputElement()…type='number'…placeholder='price'…style.width='50px';
LineQty.onKeyUp.listen((e){if(LineQty.checkValidity()==false)LineQty.value='0';});
listen((e){if(Lineprice.checkValidity()==false)Lineprice.value='0';});
影子主机
..style.position='relative'
…style.display='inline-block'
..style.verticalAlign='top'
…style.backgroundColor='#ffffff'
…style.boxShadow='1px 1px 5px#333333'
..style.boxSize='边框框'
…style.marginTop='2px'
…style.padding='4px'
..style.paddingRight='30px'
…样式。边框半径='2px'
…style.fontSize='14px'
..style.transition='所有0.2秒的缓进';
clsBtn
…onClick.listen((e)=>this.remove())
..style.position='absolute'
…style.right='5px'
..style.top='5px'
…style.color='黑色'
..style.cursor='指针'
..style.zIndex='1'
..style.fontSize='16px'
..style.fontwweight='实心'
…类。添加('ion-close-circled')
…text='x'
;
shadow.nodes..add(LineItem)…add(LineQty)…add(Lineprice)…add(clsBtn);
}
在我的函数中调用,如下所示:
Future fonixFuture()=>newfuture.value(true);
对于(订单行中的变量行){
fonixFuture()
。然后(())=>LineDisplay=new getPurchaseLines())
。然后(())=>LineDisplay.getItems()
.then(()=>this.parent.nodes.add(LineDisplay))
.然后(()=>打印(this.parent.nodes));
}
并且只有最后一个元素(LineDisplay)显示正确的getItem()结果。例如,如果我有4行,LineDisplay是:
返回Future.forEach(订单行,(ol){
return fonixFuture()
。然后(()=>打印(ol))
。然后(())=>LineDisplay=new getPurchaseLines())
.然后(()=>LineDisplay..getItems())
.then(()=>this.parent.nodes.add(LineDisplay))
.然后(()=>打印(this.parent.nodes));
});
自定义元素是:
fonix_客户端_库的一部分;
类getPurchaseLines扩展了HtmleElement{
静态final标记='get POlines';
factory getPurchaseLines()=>新元素.tag(tag);
变量阴影、行项目、行数量、行价格;
getPurchaseLines.created():super.created(){
shadow=this.createShadowRoot();
LineItem=new SelectElement()
…子项。添加(新的OptionElement(值:“0”,数据:“股票代码”);
LineQty=new InputElement()…type='number'…占位符='Qty'…style.width='50px';
Lineprice=newInputElement()…type='number'…placeholder='price'…style.width='50px';
LineQty.onKeyUp.listen((e){if(LineQty.checkValidity()==false)LineQty.value='0';});
listen((e){if(Lineprice.checkValidity()==false)Lineprice.value='0';});
影子主机
..style.position='relative'
…style.display='inline-block'
..style.verticalAlign='top'
…style.backgroundColor='#ffffff'
…style.boxShadow='1px 1px 5px#333333'
..style.boxSize='边框框'
…style.marginTop='2px'
…style.padding='4px'
..style.paddingRight='30px'
…样式。边框半径='2px'
…style.fontSize='14px'
..style.transition='所有0.2秒的缓进';
shadow.nodes..add(行项目)…add(行数量)…add(行价格);
}
getItems(){
打印('getItems');
请求=新的HttpRequest();
request.onReadyStateChange.listen(onData_getItems);
打开('POST',host+'/getItems');
请求发送(“”);
}
onData_获取项目(u){
if(request.readyState!=HttpRequest.DONE)返回;
其他的
打印('收到响应..):