在 Flutter 中将字符串传递给 Class?

Pass a String to a Class in Flutter?

我在 Github 上使用 streaming_audio_flutter_demo 项目。 https://github.com/suragch/streaming_audio_flutter_demo

它有一个 class,它为我的应用程序提供了一个 ValueListenableBuilder 和一个滑块以及播放和暂停控件。

唯一的问题是,我想将示例 URL 更改为我自己的示例;

static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';

但我不确定如何将它从我的应用程序主页传递到 class。这是 PageManager class;

的代码
 class PageManager {
  final progressNotifier = ValueNotifier<ProgressBarState>(
    ProgressBarState(
      current: Duration.zero,
      buffered: Duration.zero,
      total: Duration.zero,
    ),
  );
  final buttonNotifier = ValueNotifier<ButtonState>(ButtonState.paused);

  late AudioPlayer _audioPlayer;
  static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';

  PageManager() {
    _init();
  }

  void _init() async {
    // initialize the song
    _audioPlayer = AudioPlayer();
    await _audioPlayer.setUrl(url);        
  }  
}

我需要传递的字符串如下所示;

_current?.path

那么我怎样才能访问

static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';

来自我应用程序的主页?

您可以使用构造函数。 在 PageManager class 添加这个函数:

String url;
PageManager(this.url);

then you can define an object related to this class as follow:
PageManager pageManager = new PageManager("YOUR URL");

替换

static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';

  PageManager() {
    _init();
  }

String url; // don't use const variable!
PageManager({this.url="You can set a default URL here"}){
   _init();
}

现在您可以在 main.dart 中使用它,如下所示:

_pageManager = PageManager(url: "YOUR URL");

让我知道结果。