搜索分类、插件与代码仓

after_init 封面
插件详情v0.2.0

after_init

向有状态小部件添加了一个 didInitState() 生命周期方法,您可以在其中安全地访问继承的小部件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
155
喜欢数
33
Pub 分数
160

安装插件

flutter pub add after_init

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

after_init 包通过引入一个新的生命周期方法 didInitState() 来增强 Flutter 的有状态组件,该方法在 initState() 之后立即调用,并保证继承的组件已完全可用。这使得开发者可以安全地访问依赖项或基于上下文的数据,而不会出现空值风险。该包通过提供一个可靠的初始化逻辑钩子,简化了依赖注入和状态管理流程。该包轻量且兼容所有主要平台,非常适合需要早期访问继承组件的应用。

适合场景

  • 在 initState 之后访问继承的 widget
  • 简化依赖注入设置
  • 使用上下文相关数据初始化状态
  • 避免在 initState 中进行空值检查
  • 确保安全的 widget 树遍历
  • 简化初始化逻辑

主要特点

  • 新的 didInitState() 生命周期方法
  • 对继承的 widget 的保证访问
  • 轻量且开销极小
  • 跨平台兼容性
  • 与现有代码的轻松集成
  • 无额外样板代码

适合对象

  • 使用继承小部件的开发人员
  • 具有复杂依赖注入的应用程序
  • 需要早期上下文访问的状态化小部件
  • 优先考虑干净初始化模式的团队
  • 旨在降低空安全风险的项目
  • Flutter 初学者学习生命周期方法

使用建议

  • 需要理解 Flutter 的生命周期
  • 对于没有继承依赖项的简单小部件不需要
  • 如果过度使用,可能会增加复杂性
  • 仅限有状态小部件
  • 不替代其他状态管理解决方案
  • 最好与已建立的状态管理实践结合使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。