'Looking up a deactivated widget's ancestor is unsafe error when press SnackBarAction

I have list view with download button on each item. When user click the button shwoModalBottomSheet appeared and user can choose the favourite download folder and the closed the bottom sheet. I am using the flutter_bloc and listening the DownloadSuccess state. If download is success, show the snack bar with action to open download list screen and close the bottom sheet. When I click "Show Progress" button, I got error.

FlutterError (Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
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.)

Bloc Success Event

ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text(state.successMessage),
              duration: const Duration(seconds: 3),
              action: SnackBarAction(
                  label: 'Show Progress',
                  onPressed: () {
                    pushNewScreen(context,
                        screen: DownloadPlayListScreen(
                            favourite: state.favourite),
                        pageTransitionAnimation:
                            PageTransitionAnimation.scale);
                  }),
            ),
          );
Navigator.of(context).pop();

BottomSheet

void _showModalBottomSheet(BuildContext scaffoldContext, Song song, int index) {
const double _radius = 25.0;

showModalBottomSheet(
  context: scaffoldContext,
  isScrollControlled: true,
  shape: const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(_radius),
      topRight: Radius.circular(_radius),
    ),
  ),
  builder: (context) {
    return DraggableScrollableSheet(
      maxChildSize: 0.9,
      expand: false,
      builder: (context, scrollController) {
        return Scaffold(
          body: Column(
            children: [
              Container(
                width: MediaQuery.of(context).size.width,
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
                child: AutoSizeText(
                  kDownloadTo,
                  textAlign: TextAlign.left,
                  style: Theme.of(context).textTheme.subtitle1,
                ),
              ),
              Divider(
                color: Theme.of(context).dividerColor,
              ),
              Expanded(
                child: FavouriteListView(
                  socialMode: SocialMode.download,
                  controller: scrollController,
                  currentMediaItem: currentMediaItem,
                ),
              )
            ],
          ),
        );
      },
    );
  },
).whenComplete(() {
});
}


Solution 1:[1]

try below code in your code

Navigator.of(context,rootNavigator: true).pop();

or

Navigator.of(context).pop(true);

insted of

Navigator.of(context).pop();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravindra S. Patil