Flutter get library - Unhandled Exception: NoSuchMethodError: The getter 'overlay' was called on null. E/flutter (29207): Receiver: null

Viewed 694

I want to show dialogue and Toast message without context as I am calling those from a method that has no context access. I found get library is appropriate. It mentioned that "you can open dialog from anywhere in your code without context", but when I put any simple code from example in my app code, I get errors. Here is my test code:

import 'package:get/get.dart';

void main() {
  runApp(MyApp());

  const oneSec = const Duration(milliseconds: 250);
  new Timer.periodic(oneSec, (Timer t) => check());
}

void check() {
  if (result == PI) {   //global variables
    //showDialogue();
    //Get.snackbar('Hi', 'i am a modern snackbar');//error
    Get.dialog(SimpleDialog());//error
  }
}

void showDialogue(){
}

error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'overlay' was called on null.
E/flutter (29207): Receiver: null
E/flutter (29207): Tried calling: overlay
E/flutter (29207): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (29207): #1      Get.overlayContext (package:get/src/get_main.dart:907:62)
E/flutter (29207): #2      Get.dialog (package:get/src/get_main.dart:225:16)
E/flutter (29207): #3      checkVentilator.<anonymous closure> (package:ventilator/main.dart:82:17)
E/flutter (29207): #4      _rootRunUnary (dart:async/zone.dart:1192:38)
.............................

FlutterToast library code runs fine here. But I'm always getting somekind of error with Get library code. Even if this Get code executed from onTapp function of a button built as widget. I do not want to use GetMaterialAPP() but will it fix these errors? How can I solve this problem and show simple dialogue or toast?

1 Answers

Take a look at

https://pub.dev/packages/get#installing

In my case, I simply forgot switching out the MaterialApp.

EDIT: Sorry didn't fully read the question, was just happy that I figured out the solution and wanted to share it. I tried quite a few things, but it doesn't seem like Get can get a hold of the context without GetMaterialApp.

What's the issue with GetMaterialApp anyway?

Related