1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ScaffoldMessenger エラー 解決方法

Last updated at Posted at 2022-06-22

初めに

ScaffoldMessengerを使い、全画面に戻った際にSnackBarでアラートを表示しようとした際に、エラーが出た為、解決した方法を記述します。

解決方法

エラー内容

ScaffoldMessengerを使うときはcontextに依存する為、なんかすごく大変ぽいです。
自分はこのここ理解できず、ScaffoldMessengerを使った方法では解決できませんでした。

FlutterError (Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.)

解決: Fluttertoastパッケージを使う

Fluttertoastを使う事で、context等の難しいことは考えず、今回の処理を行うことができます!

Fluttertostの追加

コマンドで下記を実行

flutter pub add fluttertoast

コードを記述

ScaffoldMessengerの記述を下記のコードに書き換えればOKです!

Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );

gravityの部分を変更すればメッセージの表示位置を変更したり、backgroundColorで背景色を変更もできるの、公式を参考にいじってみてください。
[参考URL]
https://pub.dev/packages/fluttertoast

終わりに

Twitterでも情報発信しておりますので、ぜひフォローお願い致します!
https://mobile.twitter.com/tatsuki_kt

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?