Flutter: NoSuchMethodError was thrown. The getter 'type' was called on null

Hi Im currently developing mobile app with Flutter. My program has two type of user so I need to condition the user based on type from data. So while checking the user data, a NoSuchMethodError was thrown stating the getter was called on null on red screen for a second before proceed. But the object is not null. I dont know what else to do. Can anyone help me :( The 2s exception annoys me...

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
  );

this is the error

════════ 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

Upvotes: 0

Views: 1503

Answers (1)

S&#233;bastien B
S&#233;bastien B

Reputation: 445

As the exception is telling you, you are calling type on null, meaning at some point profile is null and you are trying to call type on it. You just need to check if profile is not null before trying to use it

Upvotes: 2

Related Questions