如何在获取特定数据后更新 ListView?

How can I update the ListView after getting a certain data?

我遇到过这个问题,

有什么帮助吗?

class ListTest extends StatefulWidget {
      @override
      _ListTestState createState() => _ListTestState();
    }

class _ListTestState extends State<ListTest> {

  List<XFile> _files = [];

  Future <List<XFile>> _openFile(BuildContext context) async {
    final XTypeGroup pngTypeGroup = XTypeGroup(
      label: 'PNGs',
      extensions: ['png'],
    );
    _files = await openFiles(acceptedTypeGroups: [
      pngTypeGroup,
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: Text('Press to open images (png)'),
          onPressed: () {
              _openFile(context);
            );
          }
        ),
        Expanded(
          child: ListView.builder(
            itemCount: _files.length,
            itemBuilder: (context, index) {
              return Text(_files[index].path);
            },
          ),
        ),
      ],
    );
  }
}

您需要调用 setState 才能重建小部件。我推荐你阅读 this.

您可以尝试类似的方法:

   var _newFiles = await openFiles(acceptedTypeGroups: [
    pngTypeGroup,
  ]);
  setState(() {
    _files = _newFiles;
  });