Flutter 应用程序在发布模式下不会请求存储权限

2024-01-02

我正在使用permission_handler包来请求我的应用程序中存储的读取权限,在调试模式下一切都工作正常,当我使用“flutter run --release”运行我的代码时,但是当我将代码导出为apk时,问题就出现了不出现权限对话框。 我不知道我在这里做错了什么,请帮助我!

这是 androidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.edithautotech.edithdisplayrelease">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    
    <!-- Permissions options for the 'storage' group-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="Edith Display"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
  

这是飞镖代码

import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';

class HomeScreen extends StatefulWidget {
  static const routeName = "/gallery";
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen>
    with SingleTickerProviderStateMixin {
  PermissionStatus _storageStatus = PermissionStatus.restricted;

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

  void _initPermission() async {
    final PermissionStatus status = await _permission.request();
    setState(() {
      _storageStatus = status;
    });
  }
  void _checkPermission({Function handler}) async {
    switch (_storageStatus) {
      case PermissionStatus.granted:
        handler();
        break;
      case PermissionStatus.denied:
         Fluttertoast.showToast(msg: "Storage permission denied");
        break;
      case PermissionStatus.restricted:
         Fluttertoast.showToast(msg: "Storage permission restricted");
        break;
      case PermissionStatus.permanentlyDenied:
         Fluttertoast.showToast(msg: "Storage permission permanently denied");
        break;
      case PermissionStatus.undetermined:
         Fluttertoast.showToast(msg: "Storage permission undermined");
        break;
    }
    }
Void onUploadButtonClick(){
// showing media from device here
}

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.restaurantName,
          style: theme.appBarTheme.textTheme.headline1,
        ),),
      body: Container(color: Colors.Green),
floatingActionButton: FloatingActionButton.extended(
              onPressed: () {
                Fluttertoast.showToast(msg: "Upload Pressed");
                _checkPermission(
                  handler: () =>
                      onUploadButtonClick(context, user: widget.user),
                );
              },
              label: Text(
                'Upload',
                style: theme.textTheme.button,
              ),
              icon: Icon(
                Icons.cloud_upload,
                size: 30,
              ),
            )
          );
  }

}

我很确定我已经解决了这个问题:https://stackoverflow.com/a/64235930/2025941 https://stackoverflow.com/a/64235930/2025941

我用几种不同的方法解决了这个问题:

  1. 将 proguard-rules.pro 添加到 android/app/proguard-rules.pro
#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-keep class androidx.lifecycle.** { *; } #https://github.com/flutter/flutter/issues/58479
#https://medium.com/@swav.kulinski/flutter-and-android-obfuscation-8768ac544421
  1. 将 proguard 添加到应用程序级别 build.gradle 中的 buildTypes
buildTypes {
    release {
        profile {
            matchingFallbacks = ['debug', 'release']
        }
    minifyEnabled true
    useProguard true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    signingConfig signingConfigs.release
    }
}
lintOptions {
    disable 'InvalidPackage'
    checkReleaseBuilds false
}
  1. 您还可以尝试在终端中运行主通道:
flutter channel master
  1. 我还通过导航到 SDK 管理器 -> SDK 工具 -> 检查和下载 Google Play 服务,将 android 迁移到了 Android Studio 中的 AndroidX

  2. 我还确保编译SDK和目标SDK是29;目前,SDK 30 的permission_handler 包存在问题。

  3. 我还编辑了 kotlin 主要活动文件:

package yourpackage
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter 应用程序在发布模式下不会请求存储权限 的相关文章

随机推荐

  • Minimagick 上传期间出现错误“composite -compose Over -gravity North”

    我正在尝试通过以下方式上传图像gem carrierwave gt 1 1 0 and gem mini magick gt 4 7 0 上传图像 我收到这个错误 我正在使用此代码上传图像 MiniMagick Tool Convert n
  • Firefox 与 contextmenu 事件同时触发 click 事件

    接下来的代码记录窗口对象上触发的事件 FIDDLE https jsfiddle net Makha92 kh8roneh 1 var logEvent function var count 1 timer 0 buffer functio
  • 当我在java中加载excel时出现错误

    我正在尝试用 java 读取一个简单的 xlsx private void readExcelData String excel throws Exception FileInputStream file new FileInputStre
  • PayPal REST API 交叉参考交易与付款

    我已成功使用 REST PHP API 完成了 PayPal 付款 但是 我想知道如何将 REST 交易与 PayPal Web 用户界面交叉引用 在 REST 方面 我有付款 ID getID 在交易被批准之前收到 看起来像 PAY 5B
  • Cocos 2d-x 中带有贝塞尔曲线的圆角矩形

    是否可以使用 DrawNode 对象绘制一个圆角矩形 我认为使用贝塞尔曲线是可能的 但我做了一些尝试 但我认为我无法处理它 查看 API 我只发现这两个函数 绘制四边形贝塞尔曲线 const Vec2 origin const Vec2 c
  • RTSP 身份验证:摘要问题

    我需要向流媒体服务器验证我的 RTSP 流 挑战如下 RTSP 1 0 401 Unauthorized WWW Authenticate Digest realm Streaming Server nonce 76bfe6986d3e76
  • 如何使用 Open CV 检测哈欠

    我正在开发一个 iOS 应用程序 需要检测用户何时打哈欠 我所做的是包括 Open CV 并使用 Haar Cascade 查找面孔 然后在面孔内查找嘴巴 也使用 HaarCascade 我遇到的麻烦是 我相信像做 face y mouth
  • UWP 共享功能在 Windows 10 Mobile 中不起作用

    我使用一个按钮创建了一个非常简单的 UWP 应用程序 点击它应该显示内置的共享弹出窗口分享一个PDF file 事实上 我让它适用于 Windows 10 桌面 但不适用于移动设备 弹出窗口不会出现在屏幕上 PDF 文件以字节数组形式出现
  • 保存某个范围内的所有信息并稍后恢复

    有没有办法将字体分配给范围 假设我有一个对象 myFont 我可以写 with Range A1 Font Bold myFont Bold Size myFont Size same with other properties end w
  • 如何通过滚动更改导航栏背景?

    我是网络开发新手 对于我的一个项目 我想在用户滚动时更改导航栏的背景 我希望它看起来像这样 https www nlogic co understanding vlan hopping attacks https www nlogic co
  • Java 问题中的暴力数独求解器算法

    除了求解方法之外 算法中的一切似乎都运行良好 当它使用可解数独板执行程序时 它会说无法解决 我已经尝试了解决方法中我能想到的一切 我尝试过调试 但在测试第一行后失败 有什么建议么 这是到目前为止的完整代码 public class Sudo
  • 我应该如何对具有许多子功能的功能进行单元测试?

    我希望更好地理解我应该测试具有许多子步骤或子功能的函数 假设我有以下功能 Modify the state of class somehow public void DoSomething DoSomethingA DoSomethingB
  • Visual Studio 的 .vsmdi 文件有多重要?

    这是场景 用户 A 已通过 Visual Studio 2010 创建了单元测试 测试项目和单元测试源代码已签入版本控制 用户B 从版本控制中获取测试项目和单元测试源代码 然后 用户 B 打开测试项目并收到一条消息 加载 blah blah
  • 如何正确使用同步链接哈希图

    尝试通过子类化链接哈希映射来制作 lru 映射 地图通过 collections synchronized 运行 映射的所有用法都被同步块包围 如果它们全部被删除 单元测试也会失败 人们可能会认为它们是不必要的 因为地图是通过 collec
  • “react-router”不包含名为“BrowserRouter”的导出

    我正在使用 React router 版本 5 5 1 并尝试在我的index js file src index js 14 8 21 react router does not contain an export named Brows
  • 如何从 Linux 与 SDL 2 对 Windows 进行交叉编译

    我尝试在 Arch Linux 64 位 上使用 SDL 2 和 mingw w64 g 编译器编译一个简单的 C 程序 为此 我从以下位置下载了 SDL2 devel 2 0 4 mingw tar gzhere https www li
  • WPF:如何设置垂直滑块的动态数字范围?

    我目前正在处理一个 WPF MVVM 项目 该项目有一个由多个视图使用的用户控件 但具有不同的值范围 这是我需要的一个例子 正如您所看到的 控件必须根据我需要在滑块中显示的值以不同的行为做出响应 无论数字如何 这只是一个示例 问题之一是该项
  • 为什么在 C++ 中对模板施加类型约束是不好的?

    In 这个问题 https stackoverflow com questions 874298 c templates that accept only certain typesOP询问限制模板将接受哪些类 总结一下 Java 中的同等
  • 在 ListView 上对齐两个 TextView,一左一右,而不拉伸背景

    所以我有两个TextViews每行ListView 一个应该左对齐 另一个右对齐 两个都TextViews有一个圆角矩形作为背景 应该将文本包裹在里面 所以我想出了这个
  • Flutter 应用程序在发布模式下不会请求存储权限

    我正在使用permission handler包来请求我的应用程序中存储的读取权限 在调试模式下一切都工作正常 当我使用 flutter run release 运行我的代码时 但是当我将代码导出为apk时 问题就出现了不出现权限对话框 我