Flutter 等待数据并在gui中使用它

Flutter 等待数据并在gui中使用它,flutter,Flutter,我有一个滑块,它使用一个值来渲染自己,它在我按下一个按钮后开始渲染,但在我第一次渲染它时,它会给我一个错误,然后开始正常工作。 这就是错误: 在null上调用了getter“unseconds”。 收件人:空 试着打电话:unseconds 这是使用数据的小部件: child: Consumer<MyAudio>( builder: (context, audioPlayer, child) =>

我有一个滑块,它使用一个值来渲染自己,它在我按下一个按钮后开始渲染,但在我第一次渲染它时,它会给我一个错误,然后开始正常工作。 这就是错误:

在null上调用了getter“unseconds”。 收件人:空 试着打电话:unseconds

这是使用数据的小部件:

child: Consumer<MyAudio>(
                        builder: (context, audioPlayer, child) =>
                            SleekCircularSlider(
                          appearance: CircularSliderAppearance(
                            customWidths: CustomSliderWidths(
                              progressBarWidth: 2.0,
                              trackWidth: 1.0,
                              handlerSize: 1.0,
                              shadowWidth: 1.0,
                            ),
                            infoProperties: InfoProperties(
                              modifier: (value) => '',
                            ),
                            customColors: CustomSliderColors(
                              trackColor: Colors.grey,
                              progressBarColor: Colors.black,
                            ),
                            size: 4.0,
                            angleRange: 360,
                            startAngle: -90.0,
                          ),
                          min: 0.0,
                          max: audioPlayer.songLength.inSeconds.toDouble(),
                          initialValue:
                              audioPlayer.position.inSeconds.toDouble(),
                        ),
                      ),
                    ),
我想我应该使用一个异步函数,你认为呢? 如果您需要更多代码,请参阅我的github repo以及所有文件:
滑块位于
/lib/miniPlayer
中,值位于
/lib/model/audioPlayer
中。问题是,当小部件第一次构建时,
audioPlayer.songlegth
audioPlayer.position
空的,尽管
audioPlayer
是非空的

在这段代码中,您定义了一对侦听器,但回调是在第一次构建之后调用的

\u player.onDurationChanged.listen((持续时间d){
歌曲长度=d;
notifyListeners();
});
_player.onAudioPositionChanged.listen((持续时间p){
位置=p;
notifyListeners();
});
然后,解决方案可以是:

子项:消费者(
生成器:(上下文、音频播放器、儿童)=>SleekCircularSlider(
外观:圆形滑动外观(
自定义宽度:自定义滑块宽度(
progressBarWidth:2.0,
轨道宽度:1.0,
手柄尺寸:1.0,
阴影宽度:1.0,
),
InfoProperty:InfoProperty(
修饰符:(值)=>“”,
),
自定义颜色:自定义幻灯片颜色(
trackColor:Colors.grey,
progressBarColor:颜色。黑色,
),
尺寸:4.0,
角度范围:360度,
startAngle:-90.0,
),
最小值:0.0,
max:audioPlayer.songLength?.Unseconds?.toDouble()?0.0,
初始值:audioPlayer.position?.Unseconds?.toDouble()?0.0,
),
)

或者改用加载器。

我这样做了,实际上在gui中它没有给我红屏,很好,但它给了我这个错误
偏移量参数包含一个NaN值。”dart:ui/painting.dart':失败的断言:第43行:'
该问题类似于null情况。您必须调试代码。大多数情况下,服务器并没有以正确的格式提供数据,或者,可能是您错误地解析了数据。
class MyAudio extends ChangeNotifier {
  Duration songLength;
  Duration position;

  AudioPlayer _player = AudioPlayer();
  AudioCache cache;

  MyAudio() {
    initAudio();
  }

  initAudio() {
    cache = AudioCache(fixedPlayer: _player);
    _player.onDurationChanged.listen((Duration d) {
      songLength = d;
      notifyListeners();
    });
    _player.onAudioPositionChanged.listen((Duration p) {
      position = p;
      notifyListeners();
    });