Search code examples
flutternullglobal

Change value on global string to null


I use a global variable:

String? globalDeviceVersion;

And I set the variable when I connect via bluetooth device and it works good.

The issue is when I want to 'reset' the global value and I have tried:

setState(() {
  globals.globalDeviceVersion == null;
});
print(globals.globalDeviceVersion);

and without state:

globals.globalDeviceVersion == null;
print(globals.globalDeviceVersion);

The print in both cases just prints the previous value and the global is not set to null.


Solution

  • Try below code.

    setState(() {
      globals.globalDeviceVersion = null;
    });
    print(globals.globalDeviceVersion);
    

    and without state:

    globals.globalDeviceVersion = null;
    print(globals.globalDeviceVersion);