カテゴリー・パッケージ・リポジトリを検索

after_init のカバー
パッケージ詳細v0.2.0

after_init

状態付きウィジェットに、継承されたウィジェットに安全にアクセスできる didInitState() ライフサイクルメソッドを追加します。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
155
いいね数
33
Pub ポイント
160

パッケージをインストール

flutter pub add after_init

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

after_init パッケージは、initState() の直後に呼び出され、継承されたウィジェットが完全に利用可能であることを保証する新しいライフサイクルメソッド、didInitState() を導入することで、Flutter のステートフルウィジェットを強化します。これにより、開発者は null 値のリスクなしに依存関係やコンテキストベースのデータに安全にアクセスできます。依存関係のインジェクションと状態管理ワークフローを簡素化し、初期化ロジック用の信頼できるフックを提供します。このパッケージは軽量で、すべての主要プラットフォームと互換性があり、継承ウィジェットへの早期アクセスが必要なアプリに最適です。

用途

  • initState の後に継承されたウィジェットにアクセスする
  • 依存関係の挿入設定を簡素化する
  • コンテキスト依存のデータを使用して状態を初期化しています
  • initState での null チェックを回避する
  • 安全なウィジェットツリーの走査を保証する
  • 初期化ロジックの簡素化

主な機能

  • 新しい didInitState() ライフサイクルメソッド
  • 継承されたウィジェットへの保証されたアクセス
  • 軽量でオーバーヘッドが最小限
  • クロスプラットフォーム互換性
  • 既存コードとの簡単な統合
  • 追加のボイラープレートなし

適している対象

  • 継承ウィジェットを使用する開発者
  • 複雑な依存関係のインジェクションを持つアプリ
  • 早期のコンテキストアクセスが必要なステートフルウィジェット
  • クリーンな初期化パターンを重視するチーム
  • null安全性リスクを低減することを目指すプロジェクト
  • 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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。