LocationNotFoundException (Location with the name "GMT" doesn't exist)

Viewed 1438

i need to create local notification for scheduling, the issue is when i use setLocalLocation function to get the time zone name which equals [log] timeZoneName value is =>GMT .

the exception is:

LocationNotFoundException (Location with the name "GMT" doesn't exist)

the main function:

const MethodChannel platform = MethodChannel('dexterx.dev/example_app');
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await _configureLocalTimeZone();
runApp(MyApp());
}

_configureLocalTimeZone function:

Future<void> _configureLocalTimeZone() async {
  tz.initializeTimeZones();
  final String timeZoneName = await platform.invokeMethod('getTimeZoneName');
  log("timeZoneName value is =>$timeZoneName",error: {"name":"Value or not"});
  tz.setLocalLocation(tz.getLocation(timeZoneName));
}
2 Answers

Are you working on an emulator ? By default, there is no timezone set. So you have to choose one so the GMT will not be null.

Have you tried to change the timezone in phone's settings ?

I was working on emulator and changing timezone from United States to France stopped crashing my app

I got the same problem here.

That's probably because the emulator doesn't have a LOCALE set by default.

However, I noticed that, when tz.getLocation("GMT") fails, the setLocalLocation() also fails, and it just stays at GMT+0 anyway, so you can just catch and ignore that error, or handle the GMT by yourself...

  _setupTimezone() async {
    tz.initializeTimeZones();
    try {
      var tzName = await FlutterNativeTimezone.getLocalTimezone();
      tz.setLocalLocation(tz.getLocation(tzName));
    } catch (err) {
      // If it fails, timezone will be GMT+0 anyway, so nothing to worry about.
    }
  }

If you don't like this approach, you can go the IF way:

  var tzName = await FlutterNativeTimezone.getLocalTimezone();
  if(tzName != "GMT"){
    tz.setLocalLocation(tz.getLocation(tzName));
  }
Related