【问题标题】:How is to get a value down in the widget build other than setstate flutter除了 setstate 颤振之外,如何在小部件构建中获取值
【发布时间】:2021-06-07 14:19:23
【问题描述】:

我正在使用 Agora 设置音频和视频通话。我想在接听电话后显示通话时间。

Agora 提供带有通话时长的回调:

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

而不是像上面那样使用 setState,这基本上是每秒重建小部件,我这里有什么选择?

【问题讨论】:

    标签: flutter agora.io


    【解决方案1】:

    像这样使用 valueNotifier

    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");
    }
    
    )
    
    

    【讨论】:

    • 完美。非常感谢!
    猜你喜欢
    • 2023-03-07
    • 2020-05-30
    • 2019-01-14
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2019-12-27
    相关资源
    最近更新 更多