Search categories, packages and repositories

after_init cover
Package detailsv0.2.0

after_init

Adds a didInitState() lifecycle method to stateful widgets where you can safely access inherited widgets.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
155
Likes
33
Pub points
160

Install package

flutter pub add after_init

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The after_init package enhances Flutter's stateful widgets by introducing a new lifecycle method, didInitState(), which is called immediately after initState() and guarantees that inherited widgets are fully available. This allows developers to safely access dependencies or context-based data without risking null values. It simplifies dependency injection and state management workflows by providing a reliable hook for initialization logic. The package is lightweight and compatible across all major platforms, making it ideal for apps requiring early access to inherited widgets.

Use cases

  • Accessing inherited widgets after initState
  • Simplifying dependency injection setup
  • Initializing state with context-dependent data
  • Avoiding null checks in initState
  • Ensuring safe widget tree traversal
  • Streamlining initialization logic

Key features

  • New didInitState() lifecycle method
  • Guaranteed access to inherited widgets
  • Lightweight and minimal overhead
  • Cross-platform compatibility
  • Easy integration with existing code
  • No additional boilerplate

Suitable for

  • Developers using inherited widgets
  • Apps with complex dependency injection
  • Stateful widgets needing early context access
  • Teams prioritizing clean initialization patterns
  • Projects aiming to reduce null safety risks
  • Flutter beginners learning lifecycle methods

Considerations

  • Requires understanding of Flutter's lifecycle
  • Not needed for simple widgets without inherited dependencies
  • May add complexity if overused
  • Limited to stateful widgets only
  • Does not replace other state management solutions
  • Best used alongside established state management practices

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

after_init

Adds a didInitState() lifecycle method to stateful widgets where you can safely access inherited widgets.

InheritedWidget is heavily used throughout the Flutter framework. Many state management packages such as ScopedModel and Provider use it as well. Unfortunately, InheritedWidgets are not accessible from the initState() method of State. The Flutter documentation states:

/// You cannot use [BuildContext.inheritFromWidgetOfExactType] from this
/// method. However, [didChangeDependencies] will be called immediately
/// following this method, and [BuildContext.inheritFromWidgetOfExactType] can
/// be used there.

This package consists of a simple mixin on the State class that provides a new method called didInitState(). It is only called exactly once, after initState(), and before didChangeDependencies() and build(). You can safely access InheritedWidgets from this method, and use them to perform any setup tasks for your widget.

Method Order

With this library, State methods are invoked in the following order:

  1. initState()
  2. didInitState()← new!
  3. didChangeDependencies()
  4. build()

Learn more about the Stateful Widget Lifecycle.

Example

import 'package:flutter/material.dart';
import 'package:after_init/after_init.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: Example(),
      ),
    ),
  );
}

class Example extends StatefulWidget {
  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> with AfterInitMixin<Example> {
  late Size size;

  /// This gets called first, as usual.
  @override
  void initState() {
    super.initState();
    // Your code here
  }

  /// This gets called after initState(), only once.
  /// Safely access inherited widgets here.
  @override
  void didInitState() {
    // No need to call super.didInitState().
    // setState() is not required because build() will automatically be called by Flutter.
    size = MediaQuery.of(context).size;
  }

  /// This gets called after didInitState().
  /// And anytime the widget's dependencies change, as usual.
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // Your code here
  }

  /// Finally this gets called, as usual.
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(size.toString()),
    );
  }
}

This is just a simplistic example. You would normally do something more useful in didInitState(), such as access setup data or subscribe to a Stream that comes from an InheritedWidget.

Alternative

A typical workaround for the initState() limitation is to delay execution of the code that needs to access InheritedWidget:

@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) {
    print(MediaQuery.of(context).size);
  });
}

However, this will cause your code to execute after the build() method, which may not be what you want.

Unit Tests

This package comes with unit tests that verify that didInitState() only gets called once and that State methods are invoked in the order specified above.

Credits

This package was inspired by the after_layout package.

Categories

Source repository

Projects using this package

No data available yet.