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.
CodePudding user response:
To handle the camera permission result in Flutter, you can use the
flutter_permission_handler
package.
Here is an example of how to request camera permission and handle the result in Flutter:
import 'package:flutter/material.dart';
import 'package:flutter_permission_handler/flutter_permission_handler.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
onPressed: () async {
// Request camera permission
PermissionStatus permission = await PermissionHandler().requestPermission(PermissionGroup.camera);
// Handle the permission result
if (permission == PermissionStatus.granted) {
print('Permission granted');
// You can now use the camera
} else {
print('Permission denied');
// Handle permission denied
}
},
child: Text('Request Camera Permission'),
),
),
),
);
}
}
This code will display a button that, when pressed, will request camera permission from the user. If the user grants permission, the PermissionStatus.granted value will be returned and you can use the camera. If the user denies permission, the PermissionStatus.denied value will be returned and you can handle the permission denial accordingly.
CodePudding user response:
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(() {});
}