Flutter Navigator error

Asked

Viewed 27 times

1

Good afternoon,

I’m having a problem with Flutter’s Navigator:

Through a certain screen I’m pushing a route this way:

Provider.of<AuthProvider>(context, listen: false).getAddresses()
                              .then((value) => Navigator.of(context)
                              .pushNamed('/user_locations'));

I’m just making a request before it doesn’t have any Return and then the push of the new route.

Within this new route, by clicking on one of the available buttons, I am accessing a new Provider method:

onTap: ()  {
       Provider.of<AuthProvider>(context, listen: false).setDefaultAddress(addresses[index]['id'], context);
},

Here I am passing only one id and the context. This method is defined this way:

Future<dynamic> setDefaultAddress(int addressId, BuildContext context) async {
    updateLoading(true);
    var url = Uri.parse(API_URL + 'set-default-address');
    Map<String, String> headers = {"Content-type": "application/json"};
    String json = '{"token": "$token", "address_id": "$addressId"}';

try {
      Response response = await post(url, headers: headers, body: json);
      Map data = jsonDecode(response.body);
      await updateAddress(data);
      await updateLoading(false);
      Navigator.pop(context);
      return;
    } catch (e) {
      print(e);
      updateLoading(false);
    }
  }

everything runs smoothly or error-free except when it reaches Navigator.pop(context) where this error arises:

I/flutter (19250): Looking up a deactivated widget's ancestor is unsafe.
I/flutter (19250): At this point the state of the widget's element tree is no longer stable.
I/flutter (19250): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

I’ve looked for other topics with this theme but none of the solutions solves my problem :(

Thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.