Home > Software design >  Why I get Flutter mistake screen(red screen) when using CupertinoNavigationBarBackButton?
Why I get Flutter mistake screen(red screen) when using CupertinoNavigationBarBackButton?

Time:03-30

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)));
  },

CodePudding user response:

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?

CodePudding user response:

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(),
  );
 }
}

}

  • Related