【问题标题】:I am getting findAncestorStateOfType error in flutter . What should I do about this? I am stuck here我在 flutter 中遇到 findAncestorStateOfType 错误。我该怎么办?我被困在这里
【发布时间】:2021-07-05 13:25:05
【问题描述】:

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

我得到的错误是这个 在 null 上调用了方法“findAncestorStateOfType”。 接收方:空 尝试调用: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.*

【问题讨论】:

    标签: flutter user-interface visual-studio-code project user-experience


    【解决方案1】:

    HomeRoute 类中创建 myLayoutWidget()

    并使用

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

    而不是

     Navigator.pop(context);
    

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2020-08-03
      相关资源
      最近更新 更多