FLUTTER ECOSYSTEM

martinrybak/after_init

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

初期化後 のプロジェクト画像
Stars
27
Forks
5
最終プッシュ(UTC)
2024/05/15
プロジェクト状態
公開中
Martin Rybak GitHub avatar
GITHUB User

Martin Rybak ↗

Very Good VenturesNew York, NY
言語Dart

このリポジトリが公開するパッケージ

使用している依存関係

依存関係一覧 4 件
  • flutter{"sdk":"flutter"}
  • flutter_test開発用{"sdk":"flutter"}
  • mocktail開発用^1.0.3
  • lints開発用^4.0.0

元の README

以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。

README を開く / 閉じる

after_init

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

https://raw.githubusercontent.com/martinrybak/after_init/HEAD/after_init.png

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.