Requirements
To follow this course, it is required to know following …
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
class CamScreen extends StatelessWidget {
const CamScreen({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("LIVE"),
),
body: FutureBuilder<bool>(
future: init(),
builder: (context, snapshot) {
// when error
if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error.toString(),
),
);
}
// when waiting for the response
if (!snapshot.hasData){
return Center(
child: CircularProgressIndicator(),
)
}
return Container();
},
),
);
}
Future<bool> init() async {
final resp = await [Permission.camera, Permission.microphone].request();
final cameraPermission = resp[Permission.camera];
final microphonePermission = resp[Permission.microphone];
if (cameraPermission != PermissionStatus.granted ||
microphonePermission != PermissionStatus.granted) {
throw "The Camera Permission is not granted";
}
return true;
}
}
Dart
복사
Requesting Permissions
Future<bool> RequestPermission() async{ // Need function to request camera & microphone connection
final resp = await[Permission.camera, Permission.microphone].request(); // request both camera and microphone
// specify and define the permission
final cameraPermission = resp[Permission.camera];
final microphonePermission = resp[Permission.microphone];
if(cameraPermission != PermissionStatus.granted ||
microphonePermission != PermissionStatus.granted) {
throw "Permission not granted";
}
return true; // return true if everything worked fine
}
Dart
복사
FutureBuilder<bool>(
future: RequestPermission(), // Requesting Permission Function
builder: (context, snapshot) {
// When RequestPermission() has an error
if (snapshot.hasError){
return Center(
child: Text(snapshot.error.toString()) // show Error message directly
);
}
// When RequestPermission() is in progress
if (!snapshot.hasData) {
return Child(
child: CircularProgressIndicator(),
);
}
}
Dart
복사
