user11775939
user11775939

Reputation:

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

I have faced this issue,

Any help?

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);
            },
          ),
        ),
      ],
    );
  }
}

Upvotes: 0

Views: 40

Answers (1)

fvillalba
fvillalba

Reputation: 1058

You need to call setState in order to rebuild the widget. I recommend you to read this.

You can try something like:

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

Upvotes: 1

Related Questions