-
-
Save boeledi/4e802d0b28dffdc4c7a40cb8ba37f295 to your computer and use it in GitHub Desktop.
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
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