权限处理程序根本不给出任何响应

问题描述 投票:0回答:1

嘿,我正在尝试向我的 flutter 应用程序添加权限处理程序,但是当我单击按钮时,它在设备上完全没有任何响应(它没有显示请求权限的对话框)。我尝试制作另一个项目并在那里做同样的事情,在那里效果很好。

Restarted application in 1,738ms.
E/flutter (20650): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: com.pichillilorenzo/flutter_inappwebview
E/flutter (20650):  at io.flutter.plugin.platform.PlatformViewsController.createPlatformView(PlatformViewsController.java:513)
E/flutter (20650):  at io.flutter.plugin.platform.PlatformViewsController$1.createForPlatformViewLayer(PlatformViewsController.java:169)
E/flutter (20650):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:105)
E/flutter (20650):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:55)
E/flutter (20650):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/flutter (20650):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/flutter (20650):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/flutter (20650):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (20650):  at android.os.Handler.handleCallback(Handler.java:958)
E/flutter (20650):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (20650):  at android.os.Looper.loopOnce(Looper.java:224)
E/flutter (20650):  at android.os.Looper.loop(Looper.java:318)
E/flutter (20650):  at android.app.ActivityThread.main(ActivityThread.java:8727)
E/flutter (20650):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (20650):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
E/flutter (20650):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
E/flutter (20650): , null, null)
E/flutter (20650): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
E/flutter (20650): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650): #2      ExpensiveAndroidViewController._sendCreateMessage (package:flutter/src/services/platform_views.dart:1115:5)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650): #3      AndroidViewController.create (package:flutter/src/services/platform_views.dart:838:5)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650):
W/WindowOnBackDispatcher(20650): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher(20650): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
E/flutter (20650): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method requestPermissions on channel flutter.baseflow.com/permissions/methods)
E/flutter (20650): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650): #1      MethodChannelPermissionHandler.requestPermissions (package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart:80:9)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650): #2      PermissionActions.request (package:permission_handler/permission_handler.dart:109:10)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650): #3      _ShopsState.build.<anonymous closure> (package:pos/screens/Shops/shops.dart:83:23)
E/flutter (20650): <asynchronous suspension>
E/flutter (20650):

这是我在其中应用权限处理程序的文件

import 'package:flutter/material.dart';
import 'package:pos/Screens/OtherPages/new_shop_form.dart';
import 'package:pos/Screens/OtherPages/shop_management_screen.dart';
import 'package:pos/constants.dart';
import 'package:pos/labels.dart';
import 'package:pos/CommonWidgets/button.dart';
import 'package:pos/screens/LoginSignupScreens/SignUp/page_view.dart';
import 'package:pos/screens/LoginSignupScreens/login.dart';
import 'package:permission_handler/permission_handler.dart';

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

  @override
  _ShopsState createState() => _ShopsState();
}

class _ShopsState extends State<Shops> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: const Icon(Icons.volume_up),
            onPressed: () {},
          ),
        ],
      ),
      body: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage("assets/backgrounds/bg_image3.png"),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(),
              const ImageIcon(
                AssetImage('assets/images/shop.png'),
                size: 130,
                color: kTextColor,
              ),
              Text(
                Labels.shop(),
                style: const TextStyle(color: kTextColor, fontSize: 24),
              ),
              const SizedBox(height: 40),
              LanguageButton(
                text: Labels.addShop(),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => const Newformshop(),
                    ),
                  );
                },
                borderColor: kBorderColor1,
                textColor: kTextColor,
                width: MediaQuery.of(context).size.width * 0.8,
                height: 70,
                borderWidth: 3,
                borderRadius: 100,
                textSize: 26,
              ),
              const SizedBox(height: 20),
              LanguageButton(
                text: Labels.shop(),
                onTap: () async {
                  // Navigator.push(
                  //   context,
                  //   MaterialPageRoute(
                  //     builder: (context) => const ShopManagementScreen(),
                  //   ),
                  // );
                  PermissionStatus cameraStatus =
                      await Permission.camera.request();
                  print('Camera permission status: $cameraStatus');

                  if (cameraStatus.isGranted) {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const ShopManagementScreen(),
                      ),
                    );
                  } else if (cameraStatus.isDenied) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: Text("Camera permission denied")),
                    );
                  } else if (cameraStatus.isPermanentlyDenied) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(
                          content:
                              Text("Camera permission permanently denied")),
                    );
                    await openAppSettings(); // Open app settings
                  }
                },
                borderColor: kBorderColor1,
                textColor: kTextColor,
                width: MediaQuery.of(context).size.width * 0.8,
                height: 70,
                borderWidth: 3,
                borderRadius: 100,
                textSize: 26,
              ),
              const SizedBox(height: 10),
              LanguageButton(
                text: 'LOL1',
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => const Login(),
                    ),
                  );
                },
                borderColor: kBorderColor1,
                textColor: kTextColor,
                width: MediaQuery.of(context).size.width * 0.8,
                height: 70,
                borderWidth: 3,
                borderRadius: 100,
                textSize: 26,
              ),
              LanguageButton(
                text: 'LOL2',
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => MultiStepForm(),
                    ),
                  );
                },
                borderColor: kBorderColor1,
                textColor: kTextColor,
                width: MediaQuery.of(context).size.width * 0.8,
                height: 70,
                borderWidth: 3,
                borderRadius: 100,
                textSize: 26,
              ),
            ],
          ),
        ],
      ),
    );
  }
}

这是Android.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pos">

    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:label="pos"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        
        <activity
            android:name=".MainActivity"
            android:exported="true"
            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">
            
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />
                
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>

    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT" />
            <data android:mimeType="text/plain" />
        </intent>
    </queries>
</manifest>

依赖关系:

dependencies:
  flutter:
    sdk: flutter
  permission_handler: ^11.3.1
  # flutter_inappwebview: ^6.0.0
  youtube_player_flutter: ^9.0.1
  simple_barcode_scanner: ^0.0.8
  flutter_barcode_scanner: ^2.0.0
  mobile_scanner: ^5.0.1
  camera: ^0.10.5+9
  flutter_svg: ^2.0.10+1

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.22000.2538], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.34)
[√] Android Studio (version 2023.2)
[√] VS Code (version 1.89.1)
[√] Connected device (4 available)
[√] Network resources

我尝试制作另一个项目并在那里做同样的事情,效果很好。我还尝试运行 flutter clean 并再次构建它但不起作用:(

flutter permissions camera android-permissions user-permissions
1个回答
0
投票

您使用的权限处理程序错误,这就是您获取权限当前状态的方式

   PermissionStatus cameraStatus =
                  await Permission.camera.status;

然后检查权限是否被授予,如果权限没有被永久拒绝,那么你可以使用请求

  status = await Permission.camera.request();

这是我的代码中的示例用法

    var status = await Permission.camera.status;
    if (status.isGranted) {
      //do something
    }
    if (status.isPermanentlyDenied) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('Camera permission permanently denied'),
        ),
      );
    } else {
      status = await Permission.camera.request();
      if (status.isGranted) {
       
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Camera permission granted'),
          ),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Camera permission denied'),
          ),
        );
      }
    }
  
© www.soinside.com 2019 - 2024. All rights reserved.