Flutter:抛出了 NoSuchMethodError。在 null 上调用了 getter 'type'

Posted

技术标签:

【中文标题】Flutter:抛出了 NoSuchMethodError。在 null 上调用了 getter \'type\'【英文标题】:Flutter: NoSuchMethodError was thrown. The getter 'type' was called on nullFlutter:抛出了 NoSuchMethodError。在 null 上调用了 getter 'type' 【发布时间】:2021-08-14 19:23:31 【问题描述】:

您好,我目前正在使用 Flutter 开发移动应用。我的程序有两种类型的用户,所以我需要根据数据类型来调整用户。因此,在检查用户数据时,会抛出 NoSuchMethodError 说明在继续之前在红屏上的 null 上调用了 getter 一秒钟。但对象不为空。我不知道还能做什么。任何人都可以帮助我:( 2s 异常让我烦恼......

final profile = Provider.of<Profile>(context);

if (profile.type == null) 
  return Center(
    child: CircularProgressIndicator(
      backgroundColor: Colors.white,
    ),
  );
 else 
  return MaterialApp(
    home: profile.type ? SessionWrapperTutor() : SessionWrapperTutee(), //here the problem occur
  );

这是错误

════════ Exception caught by widgets library ═══════════════════════════════════

The following NoSuchMethodError was thrown building HomeWrapper(dirty, dependencies: [InheritedProvider<Profile>], state: _HomeWrapperState#91be8):

The getter 'type' was called on null.
Receiver: null
Tried calling: type

The relevant error-causing widget was HomeWrapper
When the exception was thrown, this was the stack

#0      Object.noSuchMethod
#1      _HomeWrapperState.build
#2      StatefulElement.build
#3      ComponentElement.performRebuild
#4      StatefulElement.performRebuild

【问题讨论】:

非常感谢您!我不能 .isEmpty 因为配置文件是对象。但已经找到解决方案。谢谢你的建议! :) 【参考方案1】:

正如异常告诉您的那样,您在 null 上调用 type,这意味着在某些时候 profile 为 null,而您正试图在其上调用 type。您只需要在尝试使用之前检查profile 是否不为空

【讨论】:

兄弟,如果她在 null 上调用配置文件以便她显示进度指示器 不,有一个 if 条件:if (profile.type == null)profile 在某些时候显然为空,所以很明显它会引发异常 你还是不明白,问题不是来自type,问题是profile为空,你不能在空的东西上调用.type,只是阅读异常消息.. 天哪,它成功了!耶!是的,我需要检查配置文件是否为空并返回循环的东西。最后红屏不见了。非常感谢! :''''')

以上是关于Flutter:抛出了 NoSuchMethodError。在 null 上调用了 getter 'type'的主要内容,如果未能解决你的问题,请参考以下文章

我在 Flutter 中遇到了一个问题:在构建 TextField 时抛出了以下断言,它使我遇到了一个奇怪的问题

又抛出了一个异常:A RenderFlex 右边溢出了 3.0 像素

Flutter导航到新页面时抛出错误

在构建 ItemCard(dirty) 时抛出了以下断言:在构建期间调用了 setState() 或 markNeedsBuild()

一个 RenderFlex 在底部问题上溢出了 40 个像素

颤振 - 布局