Ad

How Can I Prevent A Production Flutter App From Running In Emulators?

- 1 answer

I have no idea how to block or prevent my production application from running on emulators or softwares such as BlueStacks

Does anyone come up with a solution for this problem?

Ad

Answer

you can use package https://pub.dev/packages/device_info

add dependency in pubspec.yaml

dependencies:
  device_info: ^0.4.0+3

and here is example how to detect is it real device or not

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Is am i in matrix?')),
        body: Test(),
      ),
    );
  }
}

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _isRealDevice(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Center(child: Text('is it real device: ${snapshot.data}'));
        } else {
          return const SizedBox.shrink();
        }
      },
    );
  }

  Future<bool> _isRealDevice() async {
    AndroidDeviceInfo androidInfo = await DeviceInfoPlugin().androidInfo;
    return androidInfo.isPhysicalDevice;
  }
}

I don't have installed Genymotion but on AS emulators it shows false as intended.

Ad
source: stackoverflow.com
Ad