'Pressing Scaffold's back button:Looking up a deactivated widget's ancestor is unsafe

I get the following error when I hit scaffold's back button:

The following assertion was thrown while finalizing the widget tree: 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.

Scaffold page I use:

    class PdfShowScreen extends StatefulWidget {
      const PdfShowScreen ({
        Key? key,
        this.route,
        this.requestObj,
        this.fileNameWithoutExtension,
      }) : super(key: key);
      final String? route;
      final dynamic requestObj;
      final String? fileNameWithoutExtension;

      @override
      _PdfShowScreenState createState() => _PdfShowScreenState();
    }

    class _PdfShowScreenState extends State<PdfShowScreen> {
      late dynamic fileData;
      late PdfController pdfController;
      Future<void>? _future;

      @override
      void initState() {
        super.initState();
        _future = fetchData();
      }

      Future<void> fetchData() async {
      ...
      }

      @override
      void dispose() {
        pdfController.dispose();
        super.dispose();
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: CustomAppBar(
            title: "Pdf",
          ),
          body: FutureBuilder(
            ...
          ),
        );
      }
    }

The page where I open PdfShowScreen :


     Widget pdfBtn() {
        return IconButton(
            onPressed: () async {
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => PdfShowScreen(
                        route: "...",
                        requestObj: [
                          {"..": ..}
                        ],
                        fileNameWithoutExtension: "..")),
              );
            },
            icon: Icon(
             ...
            ));
      }

where i call pdfBtn

  Widget itemBuilder(context, it, index) {
    EFatura item = it;
    var eFaturaProvider = Provider.of<EFaturaListProvider>(context);

    return  Visibility(
          visible: item.eBelgeUuid != null ? true : false,
          child: pdfBtn(item.faturaMasterId!, item.nosu));
  }

where i call itembuilder:

  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Sidebar(),
      appBar: CustomAppBar(),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15.0),
        child: PagedListView<int, dynamic>.separated(
          pagingController: _pagingController,
          builderDelegate: PagedChildBuilderDelegate<dynamic>(
            firstPageProgressIndicatorBuilder: (ctx) => Container(
              height: 1,
              child: ShimmerList(),
            ),
            itemBuilder: (ctx, item, index) =>
                itemBuilder(ctx, item, index),
          ),
          separatorBuilder: (context, index) => Divider()
        ),
      ),
    );
  }


Sources

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

Source: Stack Overflow

Solution Source