1

I am using below method to check whether Camera permission is granted or not:

//Global variable
int cameraPermission = -1;

Future<void> checkCameraPermission() async {
cameraPermissionStatus = await Permission.camera.status;
print(">>>>>>>>>>>>>>>>>>>>>>>>>>\ncamera permission >> " +
    cameraPermissionStatus.name);
if (cameraPermissionStatus.name == "granted") {
  cameraPermission = 1;
  scheduleTimeout(4 * 1000);
} else if (cameraPermissionStatus.name == "denied") {
  cameraPermission = 2;
} else {
  cameraPermission=0;
}
    setState(() {});
}

calling above method in initState() inside addPostFrameCallback

Reflecting view in build method as below:

return Column(
            children: [
              cameraPermission==1
                  ? Expanded(flex: 2, child: buildCameraPreview())
                  : cameraPermission==2
                      ? Container(
                          child: InkWell(
                          onTap: () async {
                            await Permission.camera.request();
                          },
                          child: Padding(
                              padding: EdgeInsets.only(top: 200.h),
                              child: Text(
                                  "Tap here to grant Camera Permission")),
                        ))
                      : cameraPermission==0
                          ? Center(
                              child: InkWell(
                              onTap: () {
                                openAppSettings();
                              },
                              child: Text(
                                  "Tap here to grant Camera permission\nfrom application Settings"),
                            ))
                          : Container(),

The Issue is by default is displays "denied" view (cameraPermission==2) which is okay, But since I have called checkCameraPermission method in init inside addPostFrameCallback,

It should reflect once I grant or allow camera permission from opened permission dialog but it not reflecting view. After going back to previous screen and coming back its working.

What might be the issue? Thanks in advance.

1
  • 1
    You're displaying Camera based on the value of cameraPermission, and you did not updated the cameraPermission variable once the user has granted the access. Try setting cameraPermission to 1 inside setState. Else try calling await Permission.camera.request(); sindie the addPostFrameCallBack. Commented Dec 19, 2022 at 7:04

1 Answer 1

1

The addPostFrameCallback will be called immediately after rendering that frame and your camera permission will be still denied. So either you need to request the permission in the checkCameraPermission method. Or call it after you request the permission.

Container(
   child: InkWell(
      onTap: () async {
         await checkCameraPermission();
      },
      child: Padding(
         padding: EdgeInsets.only(top: 200.h),
         child: Text("Tap here to grant Camera Permission"),
      ),
   ),
),

And change the implementation like:

Future<void> checkCameraPermission() async {
   cameraPermissionStatus = await Permission.camera.request();
   print(">>>>>>>>>>>>>>>>>>>>>>>>>>\ncamera permission >> " + cameraPermissionStatus.name);
   if (cameraPermissionStatus.name == "granted") {
      cameraPermission = 1;
      scheduleTimeout(4 * 1000);
   } else if (cameraPermissionStatus.name == "denied") {
      cameraPermission = 2;
   } else {
      cameraPermission=0;
   }
   setState(() {});
}
5
  • and is it require audio permission also for Camera? Commented Dec 19, 2022 at 9:37
  • @JaiminModi I didn't get that question. In which platform iOS/Android ?
    – Midhun MP
    Commented Dec 19, 2022 at 10:04
  • In Android. I have used this plugin : camera: ^0.10.0+4 and it also asking for audio permission Commented Dec 19, 2022 at 10:52
  • 1
    @JaiminModi I guess it's for recording video. In Android 13 there are some permission changes.
    – Midhun MP
    Commented Dec 19, 2022 at 11:51
  • Yes @JaiminModi you have require to give audio permission for use camera. Commented Jan 20, 2023 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.