Skip to content

Code Examples

Denis edited this page Feb 10, 2025 · 1 revision

Here are some examples of flutter security toolkit usage:

Root Privileges

class HomeContent extends StatefulWidget {
  const HomeContent({super.key});

  @override
  State<HomeContent> createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {
  bool _rootPrivileges = false;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      final jailbroken =
          await ThreatDetectionCenter.areRootPrivilegesDetected();

      if (!mounted) return;

      setState(() {
        _rootPrivileges = jailbroken ?? _rootPrivileges;
      });
    } on PlatformException {
      // Do nothing
    }
  }

  @override
  Widget build(BuildContext context) {
    return _rootPrivileges ? const ErrorContent() : const AppContent();
  }
}

Hooks

class HomeContent extends StatefulWidget {
  const HomeContent({super.key});

  @override
  State<HomeContent> createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {
  bool _hooks = false;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      final hooks = await ThreatDetectionCenter.areHooksDetected();

      if (!mounted) return;

      setState(() {
        _hooks = hooks ?? _hooks;
      });
    } on PlatformException {
      // Do nothing
    }
  }

  @override
  Widget build(BuildContext context) {
    return _hooks ? const ErrorContent() : const AppContent();
  }
}

Simulator

class HomeContent extends StatefulWidget {
  const HomeContent({super.key});

  @override
  State<HomeContent> createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      final simulator = await ThreatDetectionCenter.isSimulatorDetected();
      if (simulator == true) {
        Logger.info('App is running in simulated environment');
      }
    } on PlatformException {
      // Do nothing
    }
  }

  @override
  Widget build(BuildContext context) {
    return const AppContent();
  }
}
Clone this wiki locally