Flutter fullscreen: system_chrome.dart Error

Viewed 160

I am working on my flutter application and I want to display it in fullscreen.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  runApp(const MyApp());
}

I`m using SystemChrome in the main.dart file but when I debug my code the file "system_chrome.dart" gets opened with this error:

    } else {
      assert(mode == SystemUiMode.manual && overlays != null);
      await SystemChannels.platform.invokeMethod<void>(
        'SystemChrome.setEnabledSystemUIOverlays',
        _stringify(overlays!),
      );
    }

If I use SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); in each build method of my pages individually the code gets debugged without a problem.

Thank you in advance!

1 Answers

You are setting it too early - before Flutter framework is up.

You can call WidgetsFlutterBinding.ensureInitialized() to get Flutter framework up and running, before calling your method.

Basically, change your main() to this:

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // add this line
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  runApp(MyApp());
}
Related