我在 flutter 中收到 findAncestorStateOfType 错误。我该怎么办?我被困在这里

I am getting findAncestorStateOfType error in flutter . What should I do about this? I am stuck here

如何解决Flutter中的findAncestorStateOfType错误?我无法使用这些代码导航到其他页面。这段代码有什么问题?

我得到的错误是这个 方法 'findAncestorStateOfType' 被调用为 null。 接收者:空 尝试调用:findAncestorStateOfType()

我的代码是这样的:


// entry point for the app,
// the => operator is shorthand for {} when there is only one line of code
void main() {
  runApp(MaterialApp(
    home: HomeRoute(),
  ));
}

// the root widget of our application
class HomeRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Color(0xFFFAAC98),
        appBar: AppBar(
          backgroundColor: Color(0xFFFAAC98),
        ),
        body: myLayoutWidget(),
      ),
    );
  }
}

// replace this method with code in the examples below
Widget myLayoutWidget() {
  return Container(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            width: 300,
            height: 50,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(30.0),
              color: const Color(0xff89C5CC),
            ),
            child: Center(
              child: new Text(
                'Gram Panchayat App',
                style: TextStyle(
                    fontSize: 27,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF2F3676)),
              ),
            ),
          ),
        ),
        Row(
          children: [
            Padding(
              padding: const EdgeInsets.only(right: 18.0, top: 18.0),
              child: Image.asset(
                'assets/images/human1.png',
                width: 150,
                height: 150,
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(left: 58.0),
              child: elevatedButton(),
            ),
          ],
        ),
        new Row(
          children: [
            Padding(
              padding: const EdgeInsets.only(left: 28.0),
              child: elevatedButton1(),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 58.0, left: 68.0),
              child: new Image.asset(
                'assets/images/human2.png',
                width: 200,
                height: 170,
              ),
            ),
          ],
        ),
        Padding(
          padding: const EdgeInsets.only(left: 60.0),
          child: Row(children: [
            new Image.asset(
              'assets/images/img3.png',
              width: 180,
              height: 80,
            ),
          ]),
        )
      ],
    ),
  );
}

ElevatedButton elevatedButton() => ElevatedButton(
      onPressed: () {
        BuildContext context;
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => SecondRoute()),
        );
      },
      child: Text('Citizen'),
    );

ElevatedButton elevatedButton1() =>
    ElevatedButton(onPressed: () {}, child: Text('Staff'));

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}```

*Please help me I am stuck. I have gone through many sites but couldnt find what is wrong and the solution also.*

在你的 HomeRoute class

中制作 myLayoutWidget()

并使用

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

而不是

 Navigator.pop(context);