flutter - 方法 'findAncestorStateOfType' 在 flutter dart 中被 null 调用

标签 flutter exception dart

我正在创建一个应用程序,我是 flutter , 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

最佳答案

问题(为什么上下文对您不起作用)是因为没有 的对象。构建上下文 在您的 定制卡方法

有几种方法可以实现这一点..

一种方法是添加 构建上下文 方法中的参数像这样

  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 child 会像
customCard(context, "Theory Test 1")

另一种方法是创建一个无状态小部件并在 build 方法中返回 customcard
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 child 会像......
CustomCard("Theory Test 1")

我希望这可以帮助你。

关于flutter - 方法 'findAncestorStateOfType' 在 flutter dart 中被 null 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60711890/

相关文章:

dart - 如何垂直对齐行项目?

flutter - 我可以在 CustomPaint 中使用 Canvas 在 Flutter 中绘制自定义框阴影吗?

c# - C# 中是否有用于在给定线程上引发异常的好方法

java - 如何知道确切的函数抛出异常(Java)?

python - 'PyThreadState_SetAsyncExc' 导致 'SystemError: exception Exception() not a BaseException subclass'

flutter - 状态变量的空值

flutter - 如何将 FloatingActionButton 定位在屏幕的右下角

flutter - 当前 Dart SDK 版本不满足空安全依赖

flutter - 如何在 Dart 和 Flutter 中创建 DartDoc 宏/模板?

flutter - 为什么我的自建窗口小部件的命名参数在Flutter中不起作用?