Skip to content

Instantly share code, notes, and snippets.

@boeledi
Last active March 27, 2023 08:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boeledi/4e802d0b28dffdc4c7a40cb8ba37f295 to your computer and use it in GitHub Desktop.
Save boeledi/4e802d0b28dffdc4c7a40cb8ba37f295 to your computer and use it in GitHub Desktop.
class FiltersPage extends StatefulWidget {
@override
FiltersPageState createState() => FiltersPageState();
}
class FiltersPageState extends State<FiltersPage> {
late MovieCatalogBloc _movieBloc;
double? _minReleaseDate;
double? _maxReleaseDate;
MovieGenre? _movieGenre;
bool _isInit = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
// As the context of not yet available at initState() level,
// if not yet initialized, we get the list of the
// filter parameters
if (_isInit == false){
_movieBloc = BlocProvider.of<MovieCatalogBloc>(context)!;
_getFilterParameters();
}
}
@override
Widget build(BuildContext context) {
return _isInit == false
? Container()
: Scaffold(
...
);
}
///
/// Very tricky.
///
/// As we want to be 100% BLoC compliant, we need to retrieve
/// everything from the BLoCs, using Streams...
///
/// This is ugly but to be considered as a study case.
///
void _getFilterParameters() {
late StreamSubscription subscriptionFilters;
subscriptionFilters = _movieBloc.outFilters.listen((MovieFilters filters) {
_minReleaseDate = filters.minReleaseDate.toDouble();
_maxReleaseDate = filters.maxReleaseDate.toDouble();
// Simply to make sure the subscriptions are released
subscriptionFilters.cancel();
// Now that we have all parameters, we may build the actual page
if (mounted){
setState((){
_isInit = true;
});
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment