Ad
Creating Initial State Of Radio Group Using Bloc And Freezed Libraries
I am using bloc with freezed libraries. i need when the user open the app the first radio button is chosen. i have been created an observer bloc which tolled me that the initial state and the initial event have been invoked, but when i print
print("Print me Please ${state.maybeMap(orElse: () {}, radioClickState: (mstate) => mstate.value)}");
the output is NULL
.
the bloc which implements the application layer:
class NoteBloc extends Bloc<NoteEvent, NoteState> {
NoteBloc() : super(const NoteState.initial());
@override
Stream<NoteState> mapEventToState(
NoteEvent event,
) async* {
if(state is Initial){
yield const NoteState.radioClickState(value:1);
}
}
}
the event code is:
class NoteEvent with _$NoteEvent {
const factory NoteEvent.started() = Started;
const factory NoteEvent.radioClickEvent({required int value} ) =
RadioClickEvent;
}
the state code is:
class NoteState with _$NoteState {
const factory NoteState.initial() = Initial;
const factory NoteState.radioClickState({required int value}) =
RadioClickState;
}
the code inside materialApp is:
home: BlocProvider<NoteBloc>(
lazy: false,
create: (context) => NoteBloc(),
child: const HomePage(),
),
the home page where the radio group implemented:
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocConsumer<NoteBloc, NoteState>(
listener: (context, state) {},
builder: (context, state) {
print("Print me Please ${state.maybeMap(
orElse: () {}, radioClickState: (mstate) => mstate.value)}");
return Scaffold(
// body
body: Column(
children: [
Radio<int>(
value: 1,
groupValue: state.maybeMap(
orElse: () {}, radioClickState: (mstate) => mstate.value),
onChanged: (int? value) {},
),
Radio<int>(
value: 2,
groupValue: state.maybeMap(
orElse: () {}, radioClickState: (mstate) => mstate.value),
onChanged: (int? value) {},
),
],
},
);
}
}
Ad
Answer
Your code doesn't seem to add any event to the bloc. Maybe that's the problem?
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad