0
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.

【Flutter/Dart】Providerのコンストラクタで非同期処理をする方法

Posted at

警告
Flutter初心者が応急処置的に取った方法です。
公式に推奨される方法ではないかもしれないためご注意ください。

サーバーのデータベースにあるデータを使ってProviderのクラスを初期化したかったのですが、async/awaitがコンストラクタでは直接使えないため、ハマりました。

最終的に、このようにして解決しました。

  • 非同期処理を別の関数に切り出す。私はinit関数に処理を書きました。
init() async {
// ここに処理を書く
}
  • コンストラクタ内で、このように書く
Future(() async {
    await init();
    notifyListeners();
});
0
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
0
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?