搜尋分類、套件與程式碼儲存庫

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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。