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

时间:2021-05-26 14:16:43

标签: firebase flutter dart flutter-dependencies flutter-provider

嗨,我目前正在使用 Flutter 开发移动应用程序。我的程序有两种类型的用户,所以我需要根据数据类型来调整用户。因此,在检查用户数据时,会抛出一个 NoSuchMethodError,说明在继续之前,在红屏上调用了 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

1 个答案:

答案 0 :(得分:2)

正如异常告诉您的那样,您正在对 null 调用 type,这意味着在某个时刻 profile 为 null,并且您正试图对其调用 type。您只需要在尝试使用它之前检查 profile 是否为空