flutter _overlay!=null

参考

Package:flutter/src/widgets/overlay.dart': Failed assertion: line 133 pos 12: '_overlay != null':
Asked 4 years, 8 months ago
Modified 1 year, 4 months ago
Viewed 10k times

16


entry is a instance of OverlayEntry class

entry?.remove();
Error

Package:flutter/src/widgets/overlay.dart': Failed assertion: line 133 pos 12: '_overlay != null':
dartflutter
Share
Follow
asked Jul 20, 2018 at 13:42
Ravindra Bhanderi's user avatar
Ravindra Bhanderi
2,32833 gold badges1515 silver badges2929 bronze badges
Add a comment
4 Answers
Sorted by:

Highest score (default)

12


If there is no OverlayEntry inserted to the Overlay and you try to call the remove method, you will get that error: Failed assertion: '_overlay != null'. So before removing the entry, add a condition to judge whether it is legal or not. It's more like a hack code, you can add a variable like isEntryNotNull for convenience and understandable as well. And when you insert a new OverlayEntry into Overlay, reassign it to the entry variable, then the condition will work fine.

if (entry != null) {
    
    
  entry.remove();
  entry = null;
}
Share
Follow
edited Mar 9, 2020 at 15:34
xiawi's user avatar
xiawi
1,75244 gold badges1919 silver badges2121 bronze badges
answered Mar 9, 2020 at 8:59
halface_sun's user avatar
halface_sun
33122 silver badges1111 bronze badges
2
While this code may answer the question, adding comments to explain how it works increase the long-term value of the answer. – 
xiawi
 Mar 9, 2020 at 9:22
Well, if there is no OverlayEntry inserted to the Overlay and you try to call the remove method, you will get that error: Failed assertion: '_overlay != null'. So before removing the entry, add a condition to judge whether it is legal or not. It's more like a hack code, you can add a variable like isEntryNotNull for convinience and understandable as well. And when you insert a new OverlayEntry into Overlay, reassign it to the entry variable, then the condition will work fine. – 
halface_sun
 Mar 9, 2020 at 10:10
Add a comment

12


Resolved

After lots of research a try I found below a simple solution.

entry?.remove();
entry = null;
Share
Follow
edited Mar 13, 2020 at 5:49
answered Mar 11, 2020 at 5:10
Ravindra Bhanderi's user avatar
Ravindra Bhanderi
2,32833 gold badges1515 silver badges2929 bronze badges
Add a comment

6


try this, it is working for me

 if (overlayEntry != null && overlayEntry.mounted) {
    
    
  overlayEntry?.remove();
  overlayEntry = null;
}
Share
Follow
answered Nov 22, 2021 at 10:47
saigopi.me's user avatar
saigopi.me
13.3k22 gold badges7979 silver badges5454 bronze badges
Add a comment

Report this ad

2


You can check if the overlay is mounted like this

  if (floatingDropdown?.mounted ?? false) {
    
    
    floatingDropdown?.remove();
  }
Share
Follow

猜你喜欢

转载自blog.csdn.net/weixin_44911775/article/details/129751356
今日推荐