Package:flutter/src/widgets/overlay.dart': Failed assertion: line 133 pos 12: '_overlay != null':

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;
}

Resolved

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

entry?.remove();
entry = null;

Tags:

Dart

Flutter