0

I know, many people have asked similar questions regarding this, here. But no solution is working for me, and I'm unable to understand the solution. So please someone help me out. I declared the variable user to be non-null (by adding exclamation mark). But somehow, it is returning null. Please someone help me out by tracking down how am i getting this null.

Here is auth_event.dart file -

(Line number 17 is giving the error (from what i think).)

part of 'auth_bloc.dart';

abstract class AuthEvent extends Equatable {
  const AuthEvent();

  @override
  // TODO: implement stringify
  bool? get stringify => true;

  @override
  List<Object> get props => [];
}

class AuthUserChanged extends AuthEvent {
  final auth.User? user;  //Initial Value is null

  const AuthUserChanged({required this.user});  //Now the value is changed

  @override
  List<Object> get props => [user!];  //Still, the value being shown is null
  //TODO:  List<Object> get props => [user??auth.User];
}

class AuthLogoutRequested extends AuthEvent {}

And here is auth_bloc.dart file -

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;
import 'package:flutter/cupertino.dart';
import 'package:flutter_instagram_clone_final/blocs/blocs.dart';
import 'package:flutter_instagram_clone_final/repositories/auth/auth_repository.dart';

part 'auth_event.dart';
part 'auth_state.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final AuthRepository?  _authRepository;
  StreamSubscription<auth.User?>? _userSubscription;

  AuthBloc({
    @required AuthRepository? authRepository
  }) : _authRepository = authRepository, super(AuthState.unknown()){
    _userSubscription = _authRepository!.user.listen((user) => add(AuthUserChanged(user: user)));
  }

  @override
  Future<void> close() {
    _userSubscription!.cancel();
    return super.close();
  }

  @override
  Stream<AuthState> mapEventToState (
         AuthEvent event,
      ) async* {
    if(event is AuthUserChanged) {
      yield* _mapAuthUserChangedToState(event);
    }
    else if(event is AuthLogoutRequested) {
       await _authRepository!.logOut();
    }
  }

  Stream<AuthState> _mapAuthUserChangedToState(AuthUserChanged event) async*{
    yield event.user != null
        ? AuthState.authenticate(user: event.user!)
        : AuthState.unauthenticated();
  }
}

If there is a need of more information, please comment it out, I will add that.

Shreyansh Sharma
  • 1,588
  • 2
  • 17
  • 42

1 Answers1

0

You null handling in your event class is weird.

This is your class with proper null handling:

class AuthUserChanged extends AuthEvent {
  final auth.User user;

  const AuthUserChanged({required this.user});

  @override
  List<Object> get props => [user];
}

The variable user can never be null, because you marked it with the required keyword in your constructor. Now remove any ! concerning the variable user from the rest of your code.

Your other code looks weird too. Like a conversion tool with too litle knowledge ran over it and did it all wrong. What happened? Maybe you should look at your code line by line and fix conceptual mistakes first, before you worry about the compiler and runtime.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Yeah, I was thinking about it too. Thanks for pointing it out. I will read the documentation again and try to understand null safety more. – Shreyansh Sharma Jun 07 '21 at 13:55