Receiving Exception: The getter 'id' was called on null - flutter

Hello StackOverflow users,
I got stuck and I am not able to understand why I am receiving this error. So, let me explain me what I am doing,
This is my Home Page where my body of the Scaffold is a PageView, In the PageView the first page is TimeLine(currentUser: currentUser) where currentUser stores the details of the User.
home.dart
Scaffold buildAuthScreen() {
return Scaffold(
body: PageView(
children: [
Timeline(currentUser: currentUser),
ActivityFeed(),
Upload(currentUser: currentUser),
Search(),
Profile(profileId: currentUser?.id),
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
);
}
This is TimeLine Page, in this Page I am just trying to print 'currentUser.id' and while doing that I am receiving the exception (The getter 'id' was called on null).
timeline.dart
class Timeline extends StatefulWidget {
final User currentUser;
Timeline({this.currentUser});
#override
_TimelineState createState() => _TimelineState();
}
class _TimelineState extends State<Timeline> {
#override
Widget build(context) {
print('Timeline : ${widget.currentUser.id}');
return Scaffold(
body: Text('Timeline'),
);
}
}
I was looking up the code to see why I am receiving the exception and then I want to Upload Page, the 3rd page of the PageView and tried to print 'currentUser.id' and interestingly it worked fine here although I am receiving an exception here too but after that the user.id is getting printed..
upload.dart
class Upload extends StatefulWidget {
final User currentUser;
Upload({this.currentUser});
#override
_UploadState createState() => _UploadState();
}
class _UploadState extends State<Upload> {
#override
Widget build(BuildContext context) {
print('Upload : ${widget.currentUser.id}');
return Scaffold(
body: Text('Upload'),
);
}
}
I am confused why this behaviour is happening. If anyone needs more information ping me but I provided the basic structure and I am facing this problem.
<--Update-->
Code where I set currentUser
void createUserInFirestore() async{
/* check if user exists in users.collection in database */
final GoogleSignInAccount user = googleSignIn.currentUser;
DocumentSnapshot doc = await usersRef.doc(user.id).get();
/* If the user doesn't exist then we will create account */
if(!doc.exists) {
final username = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => CreateAccount())
);
/* Create new user from userName in users collection */
usersRef.doc(user.id).set({
"id": user.id,
"username": username,
"photoUrl": user.photoUrl,
"email": user.email,
"displayName": user.displayName,
"bio": "",
"timestamp": timestamp,
});
doc = await usersRef.doc(user.id).get();
}
currentUser = User.fromDocument(doc);
print('Create User in firestore : ${currentUser.id}');
}
}
User model class
class User {
final String id;
final String username;
final String email;
final String photoUrl;
final String displayName;
final String bio;
User({
this.id,
this.username,
this.email,
this.photoUrl,
this.displayName,
this.bio,
});
factory User.fromDocument(doc) {
return User(
id: doc.data()['id'],
username: doc.data()['username'],
email: doc.data()['email'],
photoUrl: doc.data()['photoUrl'],
displayName: doc.data()['displayName'],
bio: doc.data()['bio'],
);
}
}

Related

Flutter bloc 8.x authentication not updating

Im experimenting and learning on a small project and im trying to implement an authentication workflow. Ive implemented everything and by printing my steps everything should just work fine. Im wondering why my BlocBuilder isn't going to update. The project is really small so I can provide you everything easy. Since im learning bloc, i appreciate every hint, approach and I want to thank you in advance.
terminal output when the app is starting:
flutter: building main.dart
flutter: AppLoaded()
flutter: user is NOT signed in
flutter: false
flutter: Transition { currentState: AuthInitial(), event: AppLoaded(), nextState: UnauthenticatedState() }
flutter: block says user is NOT authenticated
This is completely fine since im checking at the beginning if there is any user data valid. Now when I press on the Login Textbutton in my home.dart my Blocbuilder should show that im logged in, but it doesnt. This is the terminal output:
flutter: AppLoaded()
flutter: signed id with credentials: User{id: 1, socketId: 123, userName: Logged in User}
flutter: user is signed in
flutter: true
flutter: currentuser is not empty: User{id: 1, socketId: 123, userName: Logged in User}
flutter: Transition { currentState: AuthInitial(), event: AppLoaded(), nextState: AuthenticatedState() }
flutter: block says user is authenticated
main.dart
import 'package:fl_auth/bloc/auth/auth_bloc.dart';
import 'package:fl_auth/repositories/user_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/bloc_observer.dart';
import 'home.dart';
import 'models/auth.dart';
void main() {
BlocOverrides.runZoned(
() {
runApp(const MyApp());
},
blocObserver: SimpleBlocObserver(),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
Auth _auth = Auth.instance;
UserRepository _userRepository = UserRepository(auth: _auth);
print('building main.dart');
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BlocProvider(
create: (context) =>
AuthBloc(userRepository: _userRepository)..add(AppLoaded()),
child: Home(),
),
);
}
}
home.dart
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/auth/auth_bloc.dart';
import 'models/auth.dart';
import 'repositories/user_repository.dart';
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
Auth _auth = Auth.instance;
UserRepository _userRepository = UserRepository(auth: _auth);
AuthBloc authBloc = AuthBloc(userRepository: _userRepository);
return Scaffold(
body: SizedBox(
height: 500,
child: Column(
children: [
Container(
height: 200,
child: BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
if (state is UnauthenticatedState) {
return Center(child: Text('User is unauthenticated'));
} else if (state is AuthenticatedState) {
return Center(child: Text('YEAH logged in!'));
} else {
return Center(child: Text('something went wrong'));
}
}),
),
TextButton(
onPressed: () => {
authBloc.userRepository.signIn(),
authBloc.add(AppLoaded())
},
child: Text('Login')),
],
)));
}
}
auth_event.dart
part of 'auth_bloc.dart';
abstract class AuthEvent extends Equatable {
const AuthEvent();
#override
List<Object> get props => [];
}
class AppLoaded extends AuthEvent {}
auth_state.dart
// ignore_for_file: public_member_api_docs, sort_constructors_first
part of 'auth_bloc.dart';
abstract class AuthState extends Equatable {
const AuthState();
#override
List<Object> get props => [];
}
class AuthInitial extends AuthState {}
class AuthenticatedState extends AuthState {
User user;
AuthenticatedState({
required this.user,
});
}
class UnauthenticatedState extends AuthState {}
auth.bloc
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:fl_auth/repositories/user_repository.dart';
import '../../models/user.dart';
part 'auth_event.dart';
part 'auth_state.dart';
class AuthBloc extends Bloc<AuthEvent, AuthState> {
UserRepository userRepository;
AuthBloc({required this.userRepository}) : super(AuthInitial()) {
on<AppLoaded>((event, emit) async {
try {
var isSignedIn = await userRepository.isSignedIn();
print(isSignedIn);
if (isSignedIn) {
var user = await userRepository.getCurrentUser();
emit(AuthenticatedState(user: user));
print('block says user is authenticated');
} else {
emit(UnauthenticatedState());
print('block says user is NOT authenticated');
}
} catch (e) {
emit(UnauthenticatedState());
}
});
}
}
user_repository.dart
import 'package:fl_auth/models/user.dart';
import '../models/auth.dart';
class UserRepository {
Auth auth = Auth.instance;
UserRepository({required this.auth});
// sign in with username
//TODO: change hardcoded username to email and passwort later on
Future<User> signIn() async {
try{
await Future.delayed(Duration(seconds: 1));
var credentials = User(id: 1, socketId: '123', userName: 'Logged in User');
print('signed id with credentials: ${auth.currentUser}');
auth.currentUser = credentials;
return auth.currentUser;
}catch(e){
print(e.toString());
throw e;
}
}
// check signed in status
Future<bool> isSignedIn() async {
try{
await Future.delayed(const Duration(seconds: 1));
var currentUser = auth.currentUser;
if(currentUser.isNotEmpty){
print('user is signed in');
return true;
} else {
print('user is NOT signed in');
return false;
}
}catch(e){
print(e.toString());
throw e;
}
}
// get user
Future<User> getCurrentUser() async {
try{
await Future.delayed(const Duration(seconds: 1));
var currentUser = auth.currentUser;
if(currentUser.isNotEmpty){
print('currentuser is not empty: $currentUser');
return currentUser;
} else {
var message = 'User is empty';
print('currentuser IS empty: $currentUser');
throw message;
}
}catch(e){
print(e.toString());
throw e;
}
}
}
auth.dart
import 'user.dart';
class Auth {
/// private constructor
Auth._();
/// the one and only instance of this singleton
static final instance = Auth._();
//ChatBloc chatBloc = ChatBloc(DatabaseApi.db);
// Create a User instance. Actually it would be better if this is empty so I can notice if a user is valid or not and can react by checking if the user has values and
// if not log the user out later on
User currentUser = User.empty;
}
user.dart
import 'package:equatable/equatable.dart';
/// {#template user}
/// User model
///
/// [User.empty] represents an unauthenticated user.
/// {#endtemplate}
class User extends Equatable {
/// {#macro user}
const User({
required this.id,
this.socketId,
this.userName,
});
/// The current user's scoket id.
final String? socketId;
/// The current user's id.
final int id;
/// The current user's name (display name).
final String? userName;
/// Empty user which represents an unauthenticated user.
static const empty = User(id: 0);
/// Convenience getter to determine whether the current user is empty.
bool get isEmpty => this == User.empty;
/// Convenience getter to determine whether the current user is not empty.
bool get isNotEmpty => this != User.empty;
#override
List<Object?> get props => [id, socketId, userName];
// Convert a user into a Map. The keys must correspond to the names of the
// columns in the database.
Map<String, dynamic> toMap() {
return {
'id': id,
'socketId': socketId,
'userName': userName,
};
}
factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['id'] as int,
socketId: map['socketId'] as String,
userName: map['userName'] as String,
);
}
// Implement toString to make it easier to see information about
// each user when using the print statement.
#override
String toString() {
return 'User{id: $id, socketId: $socketId, userName: $userName}';
}
}
The issue is In your OnTap Function you can call Event like this. I have checked and It's working as expected.
TextButton(
onPressed: () => {
authBloc.userRepository.signIn(),
context.read<AuthBloc>().add(AppLoaded())
},
child: const Text('Login')),

How to handle Firebase Authchanges with fetching user model in Riverpod

Hello everyone I'am working on Firebase Auth with Riverpod.
I have a StreamProvider which it is listening FirebaseAuthChanges. This works fine.
And I have a UserProvider which it is modeling user from Firebase. (This works fine too but not on first start).
final myAccountProvider = StateNotifierProvider<UserNotifier, UserModel>(
(ref) {
return UserNotifier();
},
);
class UserNotifier extends StateNotifier<UserModel> {
UserNotifier()
: super(
UserModel(
uid: '',
name: '',
email: '',
bildirim: 0,
kullaniciAdi: '',
pphoto: '',
sehir: '',
isBaker: null,
isBakeryEmpty: null,
bakeryID: '',
bakerySetupTaskLevel: null,
),
) {
getUserFields();
debugPrint('USERNOTİFİER İNİTİALİZED');
}
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final FirebaseService _firebaseService = FirebaseService();
getUserFields() async {
UserModel model =
await _firebaseService.fetchMyDatas(_firebaseAuth.currentUser!.uid);
state = model;
}
}
Here where the problem has begun:
My MaterialApp's home property is returning AuthWrapper widget which it is controlling user auth.
class AuthWrapper extends ConsumerWidget {
const AuthWrapper({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context, WidgetRef ref) {
final authState = ref.watch(authStateProvider);
final isBaker = ref.watch(myAccountProvider).isBaker;
return authState.when(
data: (user) {
if (user != null) {
if (isBaker == true) {
return const BakerRootView();
} else {
return const UserRootView();
}
} else {
return const LoginView();
}
},
error: (e, s) => Container(),
loading: () => const Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
),
);
}
}
Let's take a look for this widget. When the app start first the UserNotifier (so myAccountProvider) returning error that Null check op. used on null value
Where is that error ? On this line:
await _firebaseService.fetchMyDatas(_firebaseAuth.currentUser!.uid);
I know the problem what is. When the app initializing, without any firebase.currentUser, the UserNotifier can't return getUserField function.
Then I was think about defining UserNotifier after (user != null) condition like this:
if (user != null) {
final isBaker= ref.watch(myAccountProvider).isBaker;
if (isBaker== true) {
print('Baker Root Builded')
return const BakerRootView();
} else {
print('User Root Builded')
return const UserRootView();
}
} else {
return const LoginView();
}
At the first look this work's fine. But if I hot restart my app, in console, I see print('Baker Root Builded') and print('User Root Builded') together. Why is this so bad?
Because if the internet connection is not fast enough, UserRootView will be shown to eachuser.
Is there a better solution for modeling user with Authentication or not ? Any document, any article to you can show me ? Or How can I fix this issue ?

Why can't I access the User's uid from the bloc's state in this example?

After successfully signing in to Firestore using the flutterfire_ui pacakge, the user is taken to HomeScreen where initState adds a GetUser event, which eventually causes the UserBloc to yield a state object called UserLoaded with a property called activeUser, which should contain a User object with a uid property. However, when I try to access state.activeUser.uid from inside the Blockbuilder, it throws the following error:
The getter 'uid' isn't defined for the class 'Stream<User?>'.
lib/screens/home_page.dart:38
'Stream' is from 'dart:async'.
'User' is from 'package:firebase_practice/models/user.dart' ('lib/models/user.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'uid'.
'HomeScreen state is: ${state.activeUser?.uid}',
Is this because I'm using both flutterfire_ui and FirebaseAuth? Any help would be greatly appreciated.
User Model
class User {
final uid;
final userName;
final email;
User({required this.uid, this.userName, this.email});
}
AuthService:
import 'package:firebase_auth/firebase_auth.dart' as auth;
import 'package:firebase_practice/models/user.dart';
class AuthService {
final auth.FirebaseAuth _firebaseAuth;
AuthService({auth.FirebaseAuth? firebaseAuth})
: _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;
//create a dart User from Firebase user
User? _userFromFirebaseAuth(auth.User? user) {
return User(uid: user!.uid, email: user!.email);
}
Stream<User?>? get user {
return _firebaseAuth.authStateChanges().map(_userFromFirebaseAuth);
}
UserBloc:
class UserBloc extends Bloc<UserEvent, UserState> {
final AuthService _authService;
UserBloc( this._authService) : super(UserInitial()) {
on<GetUser>(_getUser);
}
FutureOr<void> _getUser(GetUser event, Emitter<UserState> emit) async {
Stream<User?>? user = await _authService.user;
if(user != null){
emit(UserLoaded(activeUser: user));
}
}
}
UserState:
class UserLoaded extends UserState {
Stream<User?> activeUser;
UserLoaded({required this.activeUser});
#override
List<Object> get props => [activeUser];
}
HomeScreen:
class HomeScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return BlocBuilder<UserBloc, UserState>(
builder: (context, state) {
if (state is UserLoaded) {
return Scaffold(
body: Center(
child: Text(
'HomeScreen with state is: ${state.activeUser.uid}',
style: TextStyle(fontSize: 40),
),
),
);
}
return CircularProgressIndicator();
},
);
}
}
You can't access the 'uid' directly because 'activeUser' is a stream of 'user'. So you could wrap your Text-widget with a StreamBuilder and provide 'state.activeUser' as the stream:
StreamBuilder(
stream: state.activeUser,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data?.uid : "");
}
return Text("");
},
);
But I have a question there, why do you save the stream itself inside your UserState? Why not save only the User and emit a new state whenever authStateChanges fires? You could do something like this:
class UserBloc extends Bloc<UserEvent, UserState> {
final AuthService _authService;
StreamSubscription<User?> _userSubscription;
UserBloc(this._authService) : super(UserInitial()) {
on<GetUser>(_getUser);
}
void _getUser(GetUser event, Emitter<UserState> emit) {
_userSubscription ??= _authService.user.listen((user) {
emit(UserLoaded(activeUser: user));
});
}
}
So you can change the UserState to hold a User? instead of a stream and you can access it directly inside you widget how you did it in your sample.
Attention: The code samples are only from my memory and probably wont work out of the box.

Future Provider Stuck In loading state

I am using a future provider to display a login page on load and then a loading indicator on loading. Here is my future provider
final loginProvider = FutureProvider.family((ref, UserInput input) =>
ref.read(authRepositoryProvider).doLogin(input.email, input.password));
In my UI I have this....
class LoginScreen extends HookWidget {
final TextEditingController emailEditingController = TextEditingController();
final TextEditingController passwordEditingController =
TextEditingController();
#override
Widget build(BuildContext context) {
var userInput =
UserInput(emailEditingController.text, passwordEditingController.text);
final login = useProvider(loginProvider(userInput));
return login.when(
data: (user) => Login(emailEditingController, passwordEditingController),
loading: () => const ProgressIndication(),
error: (error, stack) {
if (error is DioError) {
return Login(emailEditingController, passwordEditingController);
} else {
return Login(emailEditingController, passwordEditingController);
}
},
);
}
}
here is my doLogin function.
#override
Future<dynamic> doLogin(String email, String password) async {
try {
final response = await _read(dioProvider)
.post('$baseUrl/login', data: {'email': email, 'password': password});
final data = Map<String, dynamic>.from(response.data);
return data;
} on DioError catch (e) {
return BadRequestException(e.error);
} on SocketException {
return 'No Internet Connection';
}
}
I would like to know why it's stuck in the loading state. Any help will be appreciated.
First off, family creates a new instance of the provider when given input. So in your implementation, any time your text fields change, you're generating a new provider and watching that new provider. This is bad.
In your case, keeping the UserInput around for the sake of accessing the login state doesn't make a lot of sense. That is to say, in this instance, a FamilyProvider isn't ideal.
The following is an example of how you could choose to write it. This is not the only way you could write it. It is probably easier to grasp than streaming without an API like Firebase that handles most of that for you.
First, a StateNotifierProvider:
enum LoginState { loggedOut, loading, loggedIn, error }
class LoginStateNotifier extends StateNotifier<LoginState> {
LoginStateNotifier(this._read) : super(LoginState.loggedOut);
final Reader _read;
late final Map<String, dynamic> _user;
static final provider =
StateNotifierProvider<LoginStateNotifier, LoginState>((ref) => LoginStateNotifier(ref.read));
Future<void> login(String email, String password) async {
state = LoginState.loading;
try {
_user = await _read(authRepositoryProvider).doLogin(email, password);
state = LoginState.loggedIn;
} catch (e) {
state = LoginState.error;
}
}
Map<String, dynamic> get user => _user;
}
This allows us to have manual control over the state of the login process. It's not the most elegant, but practically, it works.
Next, a login screen. This is as barebones as they get. Ignore the error parameter for now - it will be cleared up in a moment.
class LoginScreen extends HookWidget {
const LoginScreen({Key? key, this.error = false}) : super(key: key);
final bool error;
#override
Widget build(BuildContext context) {
final emailController = useTextEditingController();
final passwordController = useTextEditingController();
return Column(
children: [
TextField(
controller: emailController,
),
TextField(
controller: passwordController,
),
ElevatedButton(
onPressed: () async {
await context.read(LoginStateNotifier.provider.notifier).login(
emailController.text,
passwordController.text,
);
},
child: Text('Login'),
),
if (error) Text('Error signing in'),
],
);
}
}
You'll notice we can use the useTextEditingController hook which will handle disposing of those, as well. You can also see the call to login through the StateNotifier.
Last but not least, we need to do something with our fancy new state.
class AuthPage extends HookWidget {
const AuthPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final loginState = useProvider(LoginStateNotifier.provider);
switch (loginState) {
case LoginState.loggedOut:
return LoginScreen();
case LoginState.loading:
return LoadingPage();
case LoginState.loggedIn:
return HomePage();
case LoginState.error:
return LoginScreen(error: true);
}
}
}
In practice, you're going to want to wrap this in another widget with a Scaffold.
I know this isn't exactly what you asked, but thought it might be helpful to see another approach to the problem.

Flutter BLOC state updating but always null value

I update state bloc builder context like this context.read<SurveyBloc>().add(SurveyModeChanged(mode: 'draft')); in the bloc file state changing is triggered but value always null. the last 2days I struck with this someone please help to resolve this issue.
if (event is SurveyModeChanged) {
print('mode==>');
print(state.mode);
yield state.copyWith(mode: state.mode);
}
This is Survey screen file
class SurveyView extends StatefulWidget {
#override
State<StatefulWidget> createState() => _SurveyViewState();
}
class _SurveyViewState extends State<SurveyView> {
#override
Widget build(BuildContext context) {
final sessionCubit = context.read<SessionCubit>();
return BlocProvider(
create: (context) => SurveyBloc(
user: sessionCubit.selectedUser ?? sessionCubit.currentUser,
surveyId: '4aa842ff-2b7d-4364-9669-29c200a3fe9b',
dataRepository: context.read<DataRepository>(),
),
child: BlocListener<SurveyBloc, SurveyState>(
listener: (context, state) {},
child: Scaffold(
backgroundColor: Color(0xFFF2F2F7),
appBar: _appbar(),
body: stepFormContainer(context),
resizeToAvoidBottomInset: false,
),
),
);
}
Widget saveButton() {
return BlocBuilder<SurveyBloc, SurveyState>(builder: (context, state) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: ElevatedButton.icon(
onPressed: () {
context
.read<SurveyBloc>()
.add(SurveyModeChanged(mode: 'draft'));
},
label: Text('Save')));
});
}
}
This is my Survey event code
abstract class SurveyEvent {}
class SurveyResultChanged extends SurveyEvent {
final String surveyResult;
SurveyResultChanged({this.surveyResult});
}
class SurveyModeChanged extends SurveyEvent {
final String mode;
SurveyModeChanged({this.mode});
}
class SurveyIdChanged extends SurveyEvent {
final String surveyId;
SurveyIdChanged({this.surveyId});
}
class SaveSurveyChanges extends SurveyEvent {}
Survey State dart
class SurveyState {
final User user;
final FormSubmissionStatus formSubmissionStatus;
final String surveyId;
final String mode;
final String surveyResult;
SurveyState(
{#required User user,
#required String surveyId,
String mode,
String surveyResult,
this.formSubmissionStatus = const InitialFormStatus()})
: this.user = user,
this.surveyId = surveyId,
this.mode = mode,
this.surveyResult = surveyResult;
SurveyState copyWith({
User user,
FormSubmissionStatus formSubmissionStatus,
String surveyId,
String mode,
String surveyResult,
}) {
return SurveyState(
user: user ?? this.user,
surveyId: surveyId ?? this.surveyId,
mode: mode ?? this.mode,
surveyResult: surveyResult ?? this.surveyResult,
formSubmissionStatus:
formSubmissionStatus ?? this.formSubmissionStatus);
}
}
SurveyBloc.dart
class SurveyBloc extends Bloc<SurveyEvent, SurveyState> {
final DataRepository dataRepository;
SurveyBloc({
#required User user,
#required String surveyId,
this.dataRepository,
}) : super(SurveyState(user: user, surveyId: surveyId));
#override
Stream<SurveyState> mapEventToState(SurveyEvent event) async* {
if (event is SurveyModeChanged) {
print('mode==>');
print(state.mode);
yield state.copyWith(mode: state.mode);
}
}
}
class SurveyBloc extends Bloc<SurveyEvent, SurveyState> {
final DataRepository dataRepository;
SurveyBloc({
#required User user,
#required String surveyId,
this.dataRepository,
}) : super(SurveyState(user: user, surveyId: surveyId));
#override
Stream<SurveyState> mapEventToState(SurveyEvent event) async* {
if (event is SurveyModeChanged) {
print('mode==>');
print(state.mode);
// This is where the problem occurs. You are emitting the state
// value again and again which is null. Change this:
yield state.copyWith(mode: state.mode);
// into this:
yield state.copyWith(mode: event.mode);
}
}
}