Why I get Flutter mistake screen(red screen) when using CupertinoNavigationBarBackButton?

Viewed 199

This is the problem that I got => The following assertion was thrown building CupertinoNavigationBarBackButton(dirty, dependencies: [_ModalScopeStatus]): CupertinoNavigationBarBackButton should only be used in routes that can be popped 'package:flutter/src/cupertino/nav_bar.dart': Failed assertion: line 1333 pos 9: 'currentRoute?.canPop == true'

I really do not understand this situation because I used previous 2 pages there is no problem but last page made problem.

Here is my related code part:(third page)

 @override
  Widget build(BuildContext context) {
    final name = basename(widget.file!.path);
    if(Platform.isIOS){
      return CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          leading: CupertinoNavigationBarBackButton(
            color: CupertinoColors.white,
          ),

This is he way how to come here: (first page)

CupertinoButton(
   padding: EdgeInsets.all(0),
   child: Icon(CupertinoIcons.list_bullet, color: CupertinoColors.white,),
   onPressed: () => {
      Navigator.push(context, CupertinoPageRoute(builder: (context) => ConsumptionReportsScreen(location: widget.location)))
   }
),

(second page)

onTap: () async {
   // final file = await PDFApi.loadNetwork(reports[index].pdfUrl);
   const url = 'https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf';
   final file = await PDFApi().loadNetwork(url);
   Navigator.push(context, CupertinoPageRoute(builder: (context) => PdfViewerScreen(file: file)));
  },
3 Answers

The error you're getting shows that the page you're currently on can't be popped.

Failed assertion: line 1333 pos 9: 'currentRoute?.canPop == true'

Are you sure there are pages in the stack that can be popped to?

first return a material app and in and inside it use CupertionoPageScaffold() as home page , i hope this will help

    import 'dart:io';

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatefulWidget {
      const MyApp({Key? key}) : super(key: key);

      @override
       State<MyApp> createState() => _MyAppState();
    }

    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
      if(Platform.isIOS){
       return MaterialApp(
        home: CupertinoPageScaffold(
          child: Container(),
        ),
      );
     }else {
      return MaterialApp(
        home: Scaffold(),
      );
     }
    }
   }

Try below code, I have try other way

CupertinoPageScaffold(
  navigationBar: CupertinoNavigationBar(
    backgroundColor: CupertinoColors.activeGreen,
    middle: const Text('Sample Code'),
    leading: GestureDetector(
      onTap: () {
        print('Button Pressed');
        Navigator.of(context).pop();
      },
      child: Icon(
        CupertinoIcons.left_chevron,
        color: CupertinoColors.white,
      ),
    ),
  ),
  child: Container(),
),

Result screen-> image

Related