flutter - 将字符串传递给 Flutter 中的类?

标签 flutter class dart static

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

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

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

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

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

 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';

从我的应用程序的主页?

最佳答案

替换

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

让我知道结果。

关于flutter - 将字符串传递给 Flutter 中的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68055971/

相关文章:

flutter - Flutter/Dart Uri无法在参数中转义冒号或斜线 “https://”

flutter - 标题条未 float 在 NestedScrollView 中的列表上

flutter - 在 channel plugins.flutter.io/image_picke 上找不到方法 pickImage 的实现

java - 扩展链表返回问题

javascript - qTip2 - 更改类更改时使用的工具提示内容

android - 播放商店警告 : play-services-safetynet (com. google.android.gms :play-services-safetynet) has reported critical issues with version 17. 0.0

flutter - 我的 AppBar 在屏幕上的图像后面,或者我看不到它,有什么建议可以在顶部显示我的 AppBar 和 buttomNavigator 并且是半透明的吗?

flutter - 如何从ExpansionTile中删除此灰线? ( flutter )

jquery - Polymer + JQuery UI + Dart:自动完成功能无法正常工作

python - 是否可以在 SQLAlchemy 中卸载声明类?