Last active
February 23, 2024 13:59
-
-
Save TheGlorySaint/9ff84c92594912e28a74d9e263aeed9a to your computer and use it in GitHub Desktop.
blocConsumer listener issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:codemagic_gui/features/a/a.dart'; | |
import 'package:codemagic_gui/features/b/b.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
class HomePage extends StatelessWidget { | |
const HomePage({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: BlocConsumer<ACubit, AState>( | |
listener: (context, state) async { | |
print(state); | |
if (state is AStateLoaded) { | |
await context.read<CubitB>().init(state.token); | |
} | |
}, | |
builder: (context, state) { | |
return Column( | |
children: [ | |
state.maybeMap( | |
uninitialized: (_) { | |
return Container(); | |
}, | |
orElse: SizedBox.shrink, | |
error: (_) { | |
return Container(); | |
}, | |
loading: (_) { | |
return const CircularProgressIndicator.adaptive(); | |
}, | |
empty: (value) { | |
return ElevatedButton( | |
onPressed: () async { | |
await context.read<CubitA>().saveToken( | |
key: 'key', | |
value: 'value', | |
); | |
}, | |
child: const Text('Submit'), | |
); | |
}, | |
loaded: (loaded) { | |
print('loaded'); | |
return const APage(); | |
}, | |
), | |
], | |
); | |
}, | |
), | |
); | |
} | |
} | |
CubitA SaveToken Function: | |
Future<void> saveToken({required String key, required String value}) async { | |
await localStorage.saveString(key: key, value: value).fold( | |
(err) => emit(AState.error(error: err)), | |
(_) async { | |
emit(AState.loaded(token: value)); | |
}, | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment