Home > Software engineering >  In Getx Flutter how can I get result from function?
In Getx Flutter how can I get result from function?

Time:06-06

I would like to get a percent making two functions.

My first function is a filtered list from my data.

int countP(String idC) {
final _ps = ps.where((element) => element.idC == idC);

return _ps.length;}

My second function comes from my first function:

int countPP(String idC) {
final _ps = ps.where((element) => element.idC == idC);
final _psPd = _ps.where((element) => element.pd != '');

return _psPd.length;}

In my view I would like to show the percent:

final percentPd =((pCtrl.countPP(idC) * 100) / pCtrl.countP(idC)).round();

I need to show result in Text:

Text(percentPd)

My question is:

How can I show the result in Text Widget using Getx, because when I open my view the first time doesn't show result, but if I refresh, yes.

I used Obx, GetX, GetBuilder in my Text.

I put my controller using Get.find() but doesn't work. I Used Get.put(Controller) and doesn't work

CodePudding user response:

You should declare percentPd like this

final percentPd = 0.obs;

percentPd.value = ((pCtrl.countPP(idC) * 100) / pCtrl.countP(idC)).round();

and then assign it's value afterwards and use Obx on the widget where you want to show it.

CodePudding user response:

In your widget use this code :

@override
Widget build(BuildContext context) {
return {
GetBuilder<yourController>(builder: (controller) 
{
    return Text(controller.percentPd)
}
}
  • Related