Reputation: 718
I'm getting the below error when trying to execute my flutter app:
======== Exception caught by widgets library =======================================================
The following _CastError was thrown building VerifyEmail(dirty, dependencies: [_InheritedProviderScope<User?>], state: _VerifyEmailState#da862):
Null check operator used on a null value
The relevant error-causing widget was:
VerifyEmail file:///Users/sas/Projects/Development/App/Flutter/netapp/lib/screens/sign_up.dart:152:61
When the exception was thrown, this was the stack:
#0 _VerifyEmailState.build (package:netapp/screens/verify_email.dart:58:40)
#1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
#4 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
...
====================================================================================================
The error causing code:
User? firebaseUser = Provider.of<User?>(context, listen: false);
String? emailAddress = firebaseUser!.email;
If I do not specify the null check operator [!] to email
then I get a syntax error.
I'm using this emailAddress
variable within a Text
widget.
Text(
emailAddress!,
textAlign: TextAlign.center,
style: GoogleFonts.roboto(
textStyle: Theme.of(context).textTheme.subtitle1!.copyWith(
color: kBrandAccentColor,
),
),
),
The code listed below is relevant to the provider present within the main.dart
file:
return MultiProvider(
providers: [
StreamProvider<User?>.value(
value: AuthService().onAuthStateChanged,
initialData: null,
),
]
)
The below code is present within my authentication service file:
Stream<User?> get onAuthStateChanged => authInstance.authStateChanges();
Versions of the packages:
firebase_core: ^1.3.0
firebase_auth: ^1.4.1
cloud_firestore: ^2.2.2
provider: ^5.0.0
Output of the flutter --version
:
Flutter 2.2.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 02c026b03c (5 weeks ago) • 2021-05-27 12:24:44 -0700
Engine • revision 0fdb562ac8
Tools • Dart 2.13.1
Upvotes: 0
Views: 474
Reputation: 14445
The firebaseUser
is still null
when you want to ask its email. By using the !
operator, you explicitly tell "I personally guarantee this object is not null", which isn't the case so it throws the exception.
Maybe you wanted to use the ?
operator instead?
String? emailAddress = firebaseUser?.email;
This will use optional chaining, which seems to be the thing you need here.
Upvotes: 1