Your Second Flutter App

Nov 30 2021 · Dart 2.13, Flutter 2.2.3, Visual Studio Code

Part 5: Meet Inherited Widgets

34. Lift State Up

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 33. Create an Inherited Widget Next episode: 35. Use Portrait Mode

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

At the moment, the CoursesPage and FilterPage each separately maintain the state of the filter in the app. Now that you have an InheritedWidget in FilterStateContainer, you can replace the separate state on the two pages with the single state that is lifted up in the widget tree. In Flutter, this is referred to lifting state up.

late FilterState state;
import '../../state/filter_state_container.dart';
groupValue: state.filterValue,
  void _handleRadioValueChange(int value) async {
    state.updateFilterValue(value ?? 0);
  }
remove import
class _FilterPageState extends State<FilterPage> {
  late FilterState state;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    state = FilterStateContainer.of(context);
  }
late FilterState state;
import '../../state/filter_state_container.dart';
future: _controller.fetchCourses(state.filterValue),
final courses = snapshot.data;
if (courses == null ||
              (snapshot.connectionState != ConnectionState.done)) {
  final _controller = CoursesController(CourseRepository());

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    state = FilterStateContainer.of(context);
  }
void main() => runApp(
      FilterStateContainer(
        child: MaterialApp(
          title: Strings.appTitle,
          theme: ThemeData(primaryColor: Colors.green.shade800),
          home: const RWCoursesApp(),
        ),
      ),
    );