Reputation: 2936
entry is a instance of OverlayEntry class
entry?.remove();
Error
Package:flutter/src/widgets/overlay.dart': Failed assertion: line 133 pos 12: '_overlay != null':
Upvotes: 17
Views: 14883
Reputation: 14938
try this, it is working for me
if (overlayEntry != null && overlayEntry.mounted) {
overlayEntry?.remove();
overlayEntry = null;
}
Upvotes: 9
Reputation: 289
You can check if the overlay is mounted like this
if (floatingDropdown?.mounted ?? false) {
floatingDropdown?.remove();
}
Upvotes: 2
Reputation: 2936
Resolved
After lots of research a try I found below a simple solution.
entry?.remove();
entry = null;
Upvotes: 21
Reputation: 429
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;
}
Upvotes: 15