Skip to content
pubscout

after_init

0.2.0, released 3 years ago by martinrybak.com

Provides a didInitState() method to access InheritedWidgets safely after initState.

WASM-ready
$ flutter pub add after_init
Source repository

What it does

Adds a mixin that calls didInitState() exactly once between initState() and didChangeDependencies(). This lets you read InheritedWidgets before the first build without using post‑frame callbacks.

AI-written summary of version 0.2.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to read InheritedWidgets during widget initialization
  • You want a deterministic lifecycle hook before the first build

Skip it when

  • You already use a state‑management solution that offers its own init hook
  • You prefer to avoid extra mixins for simple widgets

Strengths

  • Simple, zero‑dependency mixin
  • Works on all Flutter platforms
  • No code generation or build steps

Weaknesses

  • Limited to a single lifecycle hook
  • Does not replace full state‑management solutions
  • May be redundant if you already use didChangeDependencies

Setup

Kind
Flutter widgets
Effort
Low
Code generation
No
Direct dependencies
0
class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> with AfterInitMixin<MyWidget> {
  late Size size;
  @override
  void didInitState() {
    size = MediaQuery.of(context).size;
  }
  @override
  Widget build(BuildContext context) => Center(child: Text(size.toString()));
}

Alternatives

Packages that do the same job and could replace after_init.