Nosmai  Effects Nosmai  Moderation Nosmai Try-ons coming soon
Docs menu Flutter
How-to guide

Manage the camera lifecycle in Flutter

Use one NosmaiCameraPreview, rely on its app lifecycle handling, switch lenses safely, and detach the native view before navigation.

Platform Flutter SDK version 1.0.2 Updated Aug 22, 2026 Time 6 min

Prerequisites

  • A working Flutter quickstart
  • Exactly one mounted NosmaiCameraPreview
  • No Flutter camera controller owning the same device

Use the preview owner

NosmaiCameraPreview handles normal application pause and resume internally. For temporary app-owned tab transitions, the public controls are:

final nosmai = NosmaiFlutter.instance;
await nosmai.pauseCamera();
await nosmai.resumeCamera();

Do not call both these methods and a second camera plugin for the same screen.

Switch the camera

Await the switch result and keep the button disabled while it is pending:

final switched = await NosmaiFlutter.instance.switchCamera();
if (!switched) return;

The wrapper rejects operational failures and safely ignores rapid concurrent switches.

Use NosmaiCameraLifecycleMixin when the route needs an explicit asynchronous exit step:

await cleanupBeforeNavigation();
if (mounted) Navigator.of(context).pop();

The native PlatformView owns final disposal. Do not launch another global cleanup from State.dispose, because it can stop a newly mounted preview.

Verify it worked

Switch lenses, background and foreground the app, then open and close the route ten times. Confirm one preview remains mounted, the camera indicator turns off after exit, and returning does not create a second session.

Common errors

SymptomCauseFix
Camera is busyAnother plugin or duplicate preview owns the deviceDispose the old controller and keep one NosmaiCameraPreview
New screen closes immediatelyOld route cleanup raced with the new PlatformViewUse cleanupBeforeNavigation and do not start global cleanup in dispose
Switch returns falseA rapid or concurrent switch was safely ignoredKeep the current lens and re-enable the control after the pending call

Next steps

References

Was this page helpful?

Your response helps us prioritize documentation improvements.

Nosmai

An AR and AI company. Founded on the idea that the next decade of software will be visual, and that the tools for it should be within reach of anyone building.

Newsletter

Product updates and release notes. No spam.

© 2026 nosmai, inc · all rights reserved