I wanna update my state in dart, I have an event, state, and bloc file, and a funcion in another file, and when I call my function it doesnt update the state of my bloc.
this is a bloC from a widget in flutter, and when I use BlocProvider and gives a context, it works, but when I try to update it by a function that it will calling into a for x times, I need the function can update it.
//bloC
import 'package:bloc/bloc.dart';
import 'log_event.dart';
import 'log_state.dart';
class LogBloc extends Bloc<LogEvent, LogState>{
LogBloc() : super(LogState(['123333334'])) {
on<AddItemLogEvent>((event, emit) {
print('AddItemLogEvent received');
var updatedItems = List<String>.from(state.items)..addAll(['4444']);
emit(LogState(updatedItems));
});
}
}
//event
abstract class LogEvent {}
class AddItemLogEvent extends LogEvent {
}
class EraseItemsLogEvent extends LogEvent {}
//state
class LogState {
List<String> items = <String>[];
LogState(this.items);
}
//function
import 'package:app/bloc/log/log_event.dart';
import '../bloc/log/log_bloc.dart';
void addItem () async {
LogBloc bloc = LogBloc();
bloc.add(AddItemLogEvent());
}
New contributor
kshox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.