如何防止生产Flutter应用在模拟器中运行?

时间:2019-10-02 12:06:01

标签: flutter production-environment

我不知道如何阻止或阻止我的生产应用程序在诸如BlueStacks的模拟器或软件上运行

有人解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您可以使用软件包https://pub.dev/packages/device_info

在pubspec.yaml中添加依赖项

dependencies:
  device_info: ^0.4.0+3

这是如何检测它是否是真实设备的示例

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;
  }
}

我尚未安装Genymotion,但在AS模拟器上,它按预期显示false