Laurent Thomas
Laurent Thomas

Reputation: 282

flutter firebase fire authentication - error since update

Since I have updated flutter and Android Studio, I am getting an error with a code which was working fine previously. I have done some research but have not find the right solution. Please, can you help me to solve this? Many thanks

if (snapshot.connectionState == ConnectionState.done) {
          return StreamBuilder(
            stream: FirebaseAuth.instance.authStateChanges(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.active) {
                User user = snapshot.data; // error A value of type 'Object?' can't be assigned to a variable of type 'User'.
                if (user == null) {
                  return LoginPageTest();

``

Upvotes: 1

Views: 222

Answers (2)

Hemal Moradiya
Hemal Moradiya

Reputation: 2097

You have to define StreamBuilder data type. By default it Object? (with null safety). Here is your updated code, try this

if (snapshot.connectionState == ConnectionState.done) {
          return StreamBuilder<User?>(
            stream: FirebaseAuth.instance.authStateChanges(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.active) {
                User? user = snapshot.data;                    
                if (user == null) {
                  return LoginPageTest();

``

Upvotes: 1

Jorge Vieira
Jorge Vieira

Reputation: 3062

You should have activated null safety, so User user is declared to not accept null, but snapshot.data can be null so is incompatible. You can declase user as nullable, adding a ?, like bellow:

User? user = snapshot.data;

Upvotes: 2

Related Questions