0

I want to refresh my List when hitting the back button on the details page (which the List page links from). However, the method I need to run is a Future and it seems that causes some issues.

I have used this approach: https://stackoverflow.com/questions/49933272/how-to-refresh-a-page-after-back-bottun-pressed?rq=1#=

Here is a shot of the error: enter image description here

And here is the method:

  // Load list items
  Future _loadItems() async {
    // setState(() => _isLoading = true);
    List<CNotification> _listItems = new List<CNotification>();

    SharedPreferences prefs = await SharedPreferences.getInstance();
    String _usr = prefs.getString('usr');
    String _pwd = prefs.getString('pwd');
    String _communityId = prefs.getInt('communityid').toString();

    final response = await http.get(helperConnectionString +
        'GetNotifications?usr=$_usr&pwd=$_pwd&communityid=$_communityId');

    if (response.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      List data = json.decode(response.body);

      for (var i = 0; i < data.length; i++) {
        String _title = "";
        data[i]['MessageText'].toString().length > 25
            ? _title =
                data[i]['MessageText'].toString().substring(0, 25) + '...'
            : _title = data[i]['MessageText'].toString();

        DateTime entryCreated =
            DateTime.parse(data[i]['DateCreated'].toString());
        String _dRead =
            DateFormat.yMd().add_jms().format(entryCreated).toString();

        _listItems.add(new CNotification(int.parse(data[i]['Id'].toString()),
            _title, _dRead, data[i]['DateRead'] == null ? false : true));
      }

      setState(() {
        _items = _listItems;
        _isLoading = false;
      });
    } else {
      setState(() {
        _isLoading = false;
      });
      print('Error');
    }
  }

Anyone?

/Bob

Robert Benedetto
  • 1,590
  • 2
  • 29
  • 52

1 Answers1

0

Here is what you are looking for.

Navigator.of(context).push(MaterialPageRoute(builder: (context) => SomePage())).then((_) => _loadItems())

But I would recommend you to use async/await everywhere:

@override
Widget build(BuildContext context) {
  return RaisedButton(
    onPressed: _showSomePage,
    child: Text('Show some page'),
  );
}

void _showSomePage() async {
  await Navigator.of(context).push(MaterialPageRoute(
    builder: (context) => SomePage(),
  ));
  await _loadItems();
}

Future<void> _loadItems() async {
  // ...
}

Inline version:

@override
Widget build(BuildContext context) {
  return RaisedButton(
    onPressed: () async {
      await Navigator.of(context).push(MaterialPageRoute(
        builder: (context) => SomePage(),
      ));
      await _loadItems();
    },
    child: Text('Show some page'),
  );
}
boformer
  • 28,207
  • 10
  • 81
  • 66
  • Works like a charm, thanks! Out of curiosity, that does the _ in .then((_) do? – Robert Benedetto Jan 01 '19 at 14:49
  • the `_` symbolizes an unused argument (by convention), you could also use a different argument name. `then(...)` expects a callback with a single argument which is the result of the `Future`. – boformer Jan 01 '19 at 17:42
  • In Android Studio, you can press CTRL + B while the cursor is in `then` to take a look at the declaration. It helps a lot ;) – boformer Jan 01 '19 at 17:42