Home > Back-end >  The getter email isn't defined for the type Object. correcting the name to the name of an exist
The getter email isn't defined for the type Object. correcting the name to the name of an exist

Time:11-06

The getter 'email' isn't defined for the type 'Object'. Try importing the library that defines 'email', correcting the name to the name of an existing getter, or defining a getter or field named 'email'. I'm getting this error and This is my code

var userDetails = UserDetails(
    name: "",
    phNo: "",
    email: "",
    knownTechnologies: "",
    userDesc: "",
    id: "",
  );

Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          } else if (snapshot.hasError) {
            Center(
              child: Text("Something Went wrong"),
            );
          } else if (snapshot.hasData) {
            userDetails.email = snapshot.data!.email;
            userDetails.name = snapshot.data!.displayName
            userDetails.phNo = snapshot.data!.phoneNumber;
            userDetails.id = snapshot.data!.uid;
            final provider =
                Provider.of<UpdateProfileDetails>(context, listen: false);
            provider.addUsersProfileDetails(userDetails);
            return HomePage();
          } else {
            return MyPageView();
          }
          return MyPageView();
        });
  }

I had added null check for assigning the snapshot data to user details if it was not added I'm getting error like

The property 'email' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').

But after adding null check I'm getting error as

The getter 'email' isn't defined for the type 'Object'. Try importing the library that defines 'email', correcting the name to the name of an existing getter, or defining a getter or field named 'email'

AuthStateChanges /// Notifies about changes to the user's sign-in state (such as sign-in or /// sign-out).

 Stream<User?> authStateChanges() =>
      _pipeStreamChanges(_delegate.authStateChanges());

/// Notifies about changes to the user's sign-in state (such as sign-in or /// sign-out) and also token refresh events.

CodePudding user response:

you can't pass data to your object like that, try this:

} else if (snapshot.hasData) {
            User? user = snapshot.data;
            userDetails = UserDetails(
                  name: user!.displayName ?? "",
                  phNo: user!.phoneNumber ?? "",
                  email: user!.email ?? "",
                  knownTechnologies: "",
                  userDesc: "",
                  id: user!.uid ?? "",
            );
            final provider =
                Provider.of<UpdateProfileDetails>(context, listen: false);
            provider.addUsersProfileDetails(userDetails);
            return HomePage();
}
  • Related