单击IconButton时Flutter UI冻结

vptzau2j  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(142)

bounty将在6天后过期。回答此问题可获得+50声望奖励。Dimuth De Zoysa希望引起更多人对此问题的关注:嘿伙计们请帮我解决这个UI冻结问题。我奖励50赏金的人谁帮我解决这个问题。谢谢。

我是新的Flutter/ dart 。我试图开发一个音乐播放器应用程序。问题是当我的应用程序试图检索所有的mp3文件从下载文件夹。
它列出了模拟器上的所有音频(mp3)文件,但当我在设备上安装APK时,按下按钮时会卡住。
我能做什么?

  • [模拟器:安卓13提拉米苏]

[设备:Android 11 R]*

//requesting permission code
requestPermission() async {
    // Web platform don't support permissions methods.
    if (!kIsWeb) {
      bool permissionStatus = await _audioQuery.permissionsStatus();
      if (!permissionStatus) {
        await _audioQuery.permissionsRequest();
      }
      setState(() {});
    }
  }

//Button code
IconButton(
  icon: const Icon(Icons.menu_rounded, size: 30,), 
    onPressed: () {
      Navigator.push(context,MaterialPageRoute(builder: (context) => const Songs()));
},)
class _SongsState extends State<Songs> {

body: SafeArea(
          minimum: const EdgeInsets.fromLTRB(5, 10, 5, 5),
          child: Column(
            children: [
              Expanded(
                  child:ListView.builder(
                      itemCount: getSongList()[0].length,
                      itemBuilder: (BuildContext context, int index) {
                        return ListTile(
                          title: Text(getSongList()[1][index].split('/').last,style:
                          const TextStyle(
                              fontSize: 21
                          ),),
                          leading: IconButton(onPressed: (){
                            Navigator.push(context,MaterialPageRoute(
                                builder: (context) => music_player(selectedSong: getSongList()[1],selectedIndex:index)
                            ));
                          },
                              icon: const Icon(Icons.play_circle,size: 30,)),
                        

  // Removed all brackets to reduce code for SO question

  // function to retrieve all mp3's
  List getSongList(){

    Directory dir = Directory('/storage/emulated/0/Download/');
    String mp3Path = dir.toString();

    List<FileSystemEntity> _files;
    List<FileSystemEntity> _songs = [];
    List<String> _songpaths = [];
    _files = dir.listSync(recursive: true, followLinks: false);

    for(FileSystemEntity entity in _files) {
      String path = entity.path;
      if(path.endsWith('.mp3')) {
        _songs.add(entity);
        _songpaths.add(path);
      }
    }
    return [_songs,_songpaths];
  }

}

5t7ly7z5

5t7ly7z51#

当你在模拟器上运行你的应用程序时,它是在你的改进机器上运行的,它对存档结构有完整的感应。然而,当你在真正的装置上运行你的应用程序时,它可能不会有重要的同意来获得下载信封中的记录。
要解决这个问题,您需要在运行时从客户端请求关键的同意。()方法,它真正地看了一下同意状态,如果没有通过,就出售批准。您可能在应用程序未在web舞台上运行时调用此方法。这意味着permissionsRequest()程序未向装置移动。
要解决这个问题,您可以在应用程序启动时取出web stage的检查并可靠地调用requestPermission()方法。您可以以类似的方式添加一个检查,检查在尝试获取记录之前是否已放弃同意。下面是一个应该可以工作的代码的活力变体:

class _SongsState extends State<Songs> {
          bool _isPermissionGranted = false;
        
          @override
          void initState() {
            super.initState();
            requestPermission();
          }
        
          requestPermission() async {
            bool permissionStatus = await _audioQuery.permissionsStatus();
            if (!permissionStatus) {
              await _audioQuery.permissionsRequest();
            }
            setState(() {
              _isPermissionGranted = permissionStatus;
            });
          }
        
          List getSongList() {
            if (!_isPermissionGranted) {
              // Permission not granted, return empty lists
              return [[], []];
            }
        
            Directory dir = Directory('/storage/emulated/0/Download/');
            String mp3Path = dir.toString();
        
            List<FileSystemEntity> _files;
            List<FileSystemEntity> _songs = [];
            List<String> _songpaths = [];
            _files = dir.listSync(recursive: true, followLinks: false);
        
            for (FileSystemEntity entity in _files) {
              String path = entity.path;
              if (path.endsWith('.mp3')) {
                _songs.add(entity);
                _songpaths.add(path);
              }
            }
            return [_songs, _songpaths];
          }
        
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              body: SafeArea(
                minimum: const EdgeInsets.fromLTRB(5, 10, 5, 5),
                child: Column(
                  children: [
                    Expanded(
                      child: ListView.builder(
                        itemCount: getSongList()[0].length,
                        itemBuilder: (BuildContext context, int index) {
                          return ListTile(
                            title: Text(
                              getSongList()[1][index].split('/').last,
                              style: const TextStyle(fontSize: 21),
                            ),
                            leading: IconButton(
                              onPressed: () {
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) =>
                                        music_player(selectedSong: getSongList()[1], selectedIndex: index),
                                  ),
                                );
                              },
                              icon: const Icon(Icons.play_circle, size: 30),
                            ),
                          );
                        },
                      ),
                    ),
                  ],
                ),
              ),
            );
          }
        }
// check it out if you need any help let me know I am available

相关问题