在flutter dart中在null上调用了方法'findAncestorStateOfType'

时间:2020-03-16 19:09:39

标签: flutter exception dart

我正在创建一个应用程序,但我是飞镖新手,飞镖。我在“上下文”中有问题。我想替换页面/屏幕onTab()。但是我遇到了以下错误:


> ══════ Exception caught by gesture ════════════════════════ The
> following NoSuchMethodError was thrown while handling a gesture: The
> method 'findAncestorStateOfType' was called on null. Receiver: null
> Tried calling: findAncestorStateOfType<NavigatorState>()

这是我的代码:

class Theorytest extends StatelessWidget {

    Widget  customcard(String testName ){
      return Padding(
        padding: EdgeInsets.all(10.0),
        child: InkWell(
          onTap: (){

                      Navigator.of(context).pushReplacement(MaterialPageRoute(

              builder: (context) => GetTestData(),
           ),
           );
          },
          child: Material(     
            borderRadius: BorderRadius.circular(50.0),
            elevation: 20.0,
            child : Container(
              child: Column(
                children : <Widget>[
                  Padding(
                    padding: EdgeInsets.fromLTRB(5.0, 20.0, 10.0, 20.0),
                    child: Text( testName,
                    style: TextStyle(
                      fontSize: 24.0,
                    ),
                    ),
                    ),
                ],
              ),
            ),
          ),
        ),
      );
    }
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: ListView(
          children : <Widget>[
            customcard('Theory Test 1'),
            customcard('Theory Test 2'),
            customcard('Theory Test 3'),
            customcard('Theory Test 4'),
            customcard('Theory Test 5'),
            customcard('Theory Test 6'),
            customcard('Theory Test 7'),
            customcard('Theory Test 8'),
            customcard('Theory Test 9'),
            customcard('Theory Test 10'),
            customcard('Theory Test 11'),
            customcard('Theory Test 12'),
            customcard('Theory Test 13'),
            customcard('Theory Test 14'),
            customcard('Theory Test 15')          
          ]
        ),
      );
    }

这是代码错误。我不知道为什么背景不适合我。

enter image description here

1 个答案:

答案 0 :(得分:8)

问题(为什么上下文不适合您)是因为您的 customCard 方法中没有 BuildContext 对象

有几种方法可以实现这一目标。

一种方法是在您的方法中添加 BuildContext 参数

  Widget customcard(BuildContext context, String testName) {
    return Padding(
      padding: EdgeInsets.all(10.0),
      child: InkWell(
        onTap: () {
          Navigator.of(context).pushReplacement(
            MaterialPageRoute(
              builder: (context) => GetTestData(),
            ),
          );
        },
        child: Material(
          borderRadius: BorderRadius.circular(50.0),
          elevation: 20.0,
          child: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.fromLTRB(5.0, 20.0, 10.0, 20.0),
                  child: Text(
                    testName,
                    style: TextStyle(
                      fontSize: 24.0,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

然后您的ListView子级就像

customCard(context, "Theory Test 1")

另一种方法是创建一个无状态的小部件并在build方法中返回自定义卡片

class CustomCard extends StatelessWidget {
  String testName;
  CustomCard(this.testName);
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(10.0),
      child: InkWell(
        onTap: () {
          Navigator.of(context).pushReplacement(
            MaterialPageRoute(
              builder: (context) => GetTestData(),
            ),
          );
        },
        child: Material(
          borderRadius: BorderRadius.circular(50.0),
          elevation: 20.0,
          child: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.fromLTRB(5.0, 20.0, 10.0, 20.0),
                  child: Text(
                    testName,
                    style: TextStyle(
                      fontSize: 24.0,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

然后您的ListView子级就像...

CustomCard("Theory Test 1")

希望对您有帮助。