Search code examples
flutterdartflutter-layoutnavigator

Error on Navigator.push '!_debugLocked': is not true


I tried recreating a speed code by "The Flutter Way" but "'!_debuglocked': is not true" is showing up when I'm trying to navigate to the next screen. I've tried putting .pop and various other thing but they aren't working and looking at other solutions didn't really do it.

Code

@override
Widget build(BuildContext context) {
  return SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: Row(
      children: [
        RecommendPl(
            image: "assets/images/Orchideen.jpg",
            title: "Oscar",
            plant: "Orchideen",
            press: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailsScreen(),
                ),
              );
            }),
        RecommendPl(
            image: "assets/images/Orchideen.jpg",
            title: "Oscar",
            plant: "Orchideen",
            press: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailsScreen(),
                ),
              );
            }),
        RecommendPl(
            image: "assets/images/Orchideen.jpg",
            title: "Oscar",
            plant: "Orchideen",
            press: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailsScreen(),
                ),
              );
            }),
      ],
    ),
  );
}

class RecommendPl extends StatelessWidget {
  const RecommendPl({
    Key? key, required this.image, required this.title, required this.plant, required this.press,
  }) : super(key: key);

  final String image, title, plant;
  final Function press;

  @override

  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return Container(
        margin: EdgeInsets.only(
          left: kDefaultPadding,
          top: kDefaultPadding/2,
          bottom: kDefaultPadding * 2.5,
        ),
        width: size.width * 0.4,
        child: Column(
          children: <Widget>[
            Image.asset(image,
            ),
            GestureDetector(
              onTap: press(),
              child: Container(
                padding: EdgeInsets.all(kDefaultPadding / 2),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(10),
                    bottomRight: Radius.circular(10),
                  ),
                  boxShadow: [BoxShadow(offset: Offset(0, 10),
                    blurRadius: 50,
                    color: kPrimaryColor.withOpacity(0.23),
                  ),
                  ],
                ),
                child: Row(
                  children: [
                    RichText(text: TextSpan(
                        children: [
                          TextSpan(
                            text: "$title\n".toUpperCase(),
                            style: Theme.of(context).textTheme.button,
                          ),
                          TextSpan(
                              text: "$plant".toUpperCase(),
                              style: TextStyle(
                                  color: kPrimaryColor.withOpacity(0.5)
                              )
                          )
                        ]
                    )
                    )
                  ],
                ),
              ),
            )
          ],
        ));
  }
}


Error

 "======== Exception caught by widgets library =======================================================
The following assertion was thrown building Navigator-[GlobalObjectKey<NavigatorState> _WidgetsAppState#93b21](dirty, dependencies: [HeroControllerScope, UnmanagedRestorationScope], state: NavigatorState#6528f(tickers: tracking 2 tickers)):
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5203 pos 12: '!_debugLocked': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///F:/Projekte/Flutter/Cracker/lib/main.dart:17:12
When the exception was thrown, this was the stack: 
#2      NavigatorState.build (package:flutter/src/widgets/navigator.dart:5203:12)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4992:27)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15)
#5      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#7      StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#8      Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#9      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)"

Solution

  • You like to call the press method, when it gets pressed instead of build time,

    replace

    GestureDetector(
      onTap: press(),
    

    With

    GestureDetector(
      onTap: press,
    

    Or

    GestureDetector(
      onTap: (){
        press();
      }