【问题标题】:Widget function does not work in Column widget?小部件功能在列小部件中不起作用?
【发布时间】:2021-01-16 01:21:39
【问题描述】:

我正在构建一个 MusicPlayer 应用程序,所以我需要查看 musicTiles 列表,以便用户可以从内部存储中选择歌曲并播放它。因此,我通过添加按钮在 appbar 中使用了 File_Picker,用户可以从该按钮从本地存储中获取音乐。所以我在 file_picking 函数中使用了 async 和 await 关键字。我将 file_path 和 file_names 存储在 List 类型的变量中。之后,我在列小部件中使用了我的 _buidMusicTile() 函数,但它颤动了以下错误:

=> 类型“未来”不是类型“小部件”的子类型

代码:

class _MusicPageState extends State<MusicPage> {
  
  FileType _pickingType = FileType.any;
  TextEditingController _controller = TextEditingController();
  List<String> listofAudioFilesPath;
  List<String> listofAudioNames;

  @override
  void initState() {
    super.initState();
    _controller.addListener(() => _extension = _controller.text);
  }
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    //const Key centerKey = ValueKey('bottom-sliver-list');
    return Scaffold(
      backgroundColor: Colors.brown[300],
      appBar: AppBar(
        actions: [
          Container(
            width: size.width,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  IconButton(
                    icon: Icon(Icons.sort),
                    onPressed: () {},
                  ),
                  IconButton(
                    icon: Icon(Icons.add_circle),
                    onPressed: () async {
                      // _openFileExplorer();
                      FilePickerResult result = await FilePicker.platform
                          .pickFiles(allowMultiple: true);
                      if (result != null) {
                        listofAudioFilesPath = result.paths.toList();

                        listofAudioNames = result.names.toList();

                        print(listofAudioNames);
                        print(listofAudioFilesPath);
                      }
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
      body: Column(
        children: [
          Text('Music Library'),
          SizedBox(height: 10),
          Stack(
            children: [
              _buildMusicTile(listofAudioFilesPath, listofAudioNames),
            ],
          ),
        ],
      ),
    );
  }
}

_buildMusicTile(
    List<String> listofAudioFilesPath, List<String> listofAudioNames) async {
  List<String> listOfPath = await listofAudioFilesPath;
  List<String> listOfFiles = await listofAudioFilesPath;
  if (listofAudioFilesPath != null && listofAudioNames != null) {
    return ListView.separated(
      itemCount: listofAudioNames.length,
      separatorBuilder: (BuildContext context, int index) {
        return Container(
          height: 10,
          color: Colors.white,
        );
      },
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          leading: Icon(Icons.play_circle_filled),
          title: Text(
            '$listofAudioNames[$index]',
            style: TextStyle(color: Colors.black, fontSize: 25),
          ),
        );
      },
    );
  } else {
    return Center(
      child: Column(
        children: [
          Text(
            '$listofAudioNames',
            style: TextStyle(color: Colors.black, fontSize: 25),
          ),
          CircularProgressIndicator(),
        ],
      ),
    );
  }
}

【问题讨论】:

  • 尝试使用FutureBuilder

标签: flutter dart flutter-layout flutter-dependencies flutter-test


【解决方案1】:

你不能在 body 中使用 await 或者从 body 调用的方法,因为它需要在屏幕上渲染,UI 不能等待任何数据,我们需要更新它。

解决方案:

只需从方法 _buildMusicTile 中删除 awaitasync 即可

  List<String> listOfPath = listofAudioFilesPath;
  List<String> listOfFiles = listofAudioFilesPath;

并在onPressed() 中添加setState()

setState(() {
        print(listofAudioNames);
        print(listofAudioFilesPath);
  });

【讨论】:

  • Brother 它仍然给出相同的错误:在构建 MudicPage(dirty, dependencies: [MediaQuery], state: _MusicPageState#1cfac): type 'Future' is not 'Widget' 类型的子类型
  • 另外,从 _buildMusicTile() 中删除 async 关键字
  • 感谢兄弟的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2015-09-20
  • 2021-03-08
  • 2021-12-10
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
相关资源
最近更新 更多