LearnToday
LearnToday

Reputation: 2902

How is to get a value down in the widget build other than setstate flutter

I am using Agora to set up Audio and Video calling. I want to display the calling time after the call is answered.

Agora offers a callback with the call duration:

  rtcStats: (stats) {
          print('stats');
          print(stats.cpuAppUsage);
          print(stats.totalDuration);
          setState(() {
            duration = Duration(seconds: stats.totalDuration!);
          });
        },

Rather than using the setState as above, that will basically be rebuilding the widget every second, what options do I have here?

enter image description here

Upvotes: 1

Views: 82

Answers (1)

Dali Hamza
Dali Hamza

Reputation: 597

use valueNotifier like this

ValueNotifier<double?> secondsNotifier = ValueNotifier(null)
and  double become like this 
 rtcStats: (stats) {
          print('stats');
          print(stats.cpuAppUsage);
          print(stats.totalDuration);
          secondNotifier.value = stats.totalDuration
        },
in build add ValueListenableBuilder<double> (
valueListenable:secondNotifier,
build:(ctx,second,_){
 if(second == null){
   return SizedBox.shrink();
}
return Text("$second");
}

)

Upvotes: 1

Related Questions