5

In most languages, when you attempt to use a null pointer, an exception will get thrown. In Flutter however, this doesn't seem to be the case. Instead of throwing an exception, it simply stops executing the function.

void test() {
   Map<String, dynamic> testObject = null;

   // attempt to call a function on a null pointer
   var contains = testObject.containsKey("test");

   //will never execute:
   print("never prints");
}

This is fine in a production app. But when I'm developing an app, I want to know when my app attempts to access a null pointer.

Is it possible to enable some sort of 'strict' mode in Flutter during development so I can catch these situations during development?

Brad Hesse
  • 648
  • 6
  • 15
  • 3
    It does throw an exception though. I don't get what you want – Rémi Rousselet Jul 20 '18 at 23:46
  • I have myself in many cases been stuck at a situation where flutter shows error while calling any function on a null pointer. I also don't get why it didn't throw an error in your case. but the code you posted definitely should throw an error on the line where you called .containsKey("test") something like this => containsKey was called on null object . – Aman Malhotra Jul 21 '18 at 07:56
  • It doesn’t throw an exception, though. At least on my devices, it just stops executing the function wherever it encounters a null access. No log/console warning, no exception, nothing. Is this a bug? – Brad Hesse Jul 22 '18 at 04:46
  • Using Dart, you can also use the conditional operator to only continue chaining if the value is not null. `testObject?.containsKey("test")` would be null if `testObject` is null, or `true`/`false` if it isn't null. – Charles Crete Aug 08 '18 at 03:24
  • You're probably catching the exception somewhere (or maybe the flutter framework is?) You can try running the debugger: https://flutter.io/debugging/#dart-observatory-statement-level-single-stepping-debugger-and-profiler, and then run `set break-on-exceptions All` and you should see an exception thrown here, and be able to step into the exception handler to see what's going on. – Mike Fairhurst Aug 10 '18 at 17:59
  • It looks like I wasn’t seeing the exceptions because I was developing a plug-in/package, and the exceptions were apparently being handled silently! – Brad Hesse Aug 17 '18 at 02:31

1 Answers1

-1

Since Flutter launched null-safety on stable around 2020, enabling null-safety should display warnings for potential unsafe nullable values in your code. This should help you identify what's causing the NullPointerExceptions in your code.

On the other hand, the function ends when an Exception is thrown. If you'd still like the code to proceed after the Exception, catching the Exception should allow the function to continue after. I've modified the snippet you've provided to demonstrate.

void test() {
   Map<String, dynamic>? testObject;
   bool? contains;

   try{
     contains = testObject!.containsKey("test");
   } catch (e){
     print('Exception: $e');
   }

   print("Print after Exception");
}
Omatt
  • 8,564
  • 2
  • 42
  • 144
  • you are answering the question from 2018. there could have been a useful answer if would have given an intro to sound and unsound null safety. your syntax is incorrect and the example hardly makes any sense. – Olga Mar 30 '23 at 07:15