如何在Flutter函数中创建自定义的`onChanged`函数?

huangapple go评论98阅读模式
英文:

How can I make my custom onChanged in a function in flutter

问题

void callThisAPI({header}) {
_dio.request(
onSendProgress(sent, total) { //这是一个根据发送的数据而变化的监听器。
double percent=1.0*sent/total;
//我想要将这个百分比值返回到调用函数的地方,并相应地更新它。
}
)
}

现在,当调用:

callThisAPI(
header: someHeader,
onChanged(percentage) {
progressPercentage=percentage
//在这里,我想要连续接收百分比值当它发生变化时。
}
)

英文:

I am using a function for API call. I used it's parameter listener for checking sent and received progress. Now, I want to add an onChanged function which listens to sent and received values and notify them when the function is called. Like:

void callThisAPI({header}) {
  _dio.request(
    onSendProgress(sent, total) {    //This is a listener which changes according to the data sent.
      double percent=1.0*sent/total;
      //I want to return this percent value back to where function is called and updated accordingly.
    }
  )
}

Now, when calling:

callThisAPI(
  header: someHeader
  onChanged(percentage) {
     progressPercentage=percentage
    //Here I want to receive  the percentage continuously when it changes.
  }
)

If the question is not clear enough, please do tell me if I should paste my code for more understanding.

答案1

得分: 1

void callThisAPI({header, void Function(double)? onChanged}) {
  _dio.request(
    onSendProgress(sent, total) {    //这是一个根据发送的数据变化的监听器。
      double percent=1.0*sent/total;
      //我希望将这个百分比值返回到调用该函数的地方,并相应地更新。
      onChanged?.call(percent);
    }
  );
}
英文:
void callThisAPI({header, void Function(double)? onChanged}) {
  _dio.request(
    onSendProgress(sent, total) {    //This is a listener which changes according to the data sent.
      double percent=1.0*sent/total;
      //I want to return this percent value back to where function is called and updated accordingly.
      onChanged?.call(percent);
    }
  );
}

huangapple
  • 本文由 发表于 2023年7月24日 17:47:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定