top of page

[Flutter/dart]onWillPop of willPopScope doesn't work

  • 執筆者の写真: M.R
    M.R
  • 2021年8月15日
  • 読了時間: 1分

Overview


In flutter, if you don't want users to return to the previous page, you can wrap the widget with willPopScope and write a process that prevents users from returning at onWillPop. (See here for how to use it.)

However, even with this implementation, I can return normally. When I debug it, it seems that the method passed to onWillPop is not called in the first place.

Here describes how to deal with this case.



Cause


The cause seems to be that willPopScope was not at the root of the stack (that is, the source). In my app, I jumped from the login page to the main page with Navigator.push and put onWillPopScope there, so it seems that onWillPop was not called (reference)



Solution


In the case of a one-way transition like a login page, use pushReplacement or pushAndRemoveUntill so that the page is not left in the stack. (reference)


Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (BuildContext context) => MyHomePage())
)

 
 
 

あなたの買い物をサポートする
アプリ Shop Plan

iphone6.5p2.png

​いつ何を買うかの計画を立てられるアプリです。

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

靴を大切にしよう!靴管理アプリ SHOES_KEEP

納品:iPhone6.5①.png

靴の履いた回数、お手入れ回数を管理するアプリです。

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

「後で読む」を忘れないアプリ ArticleReminder

気になった​Webサイトを登録し、指定時刻にリマインダを送れるアプリです

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

お問い合わせ:Twitterまでお願いします

  • Twitter
bottom of page