I have a webview application and I use the following plugins for it.
flutter_inappwebview: ^5.3.2
qr_code_scanner: ^1.0.0
http: ^1.2.0
When I open the camera, I get a fatal error and the phone shuts down. This does not happen on iOS.
Fatal Error is:
F/flutter (10851): [FATAL:flutter/shell/platform/android/surface_texture_external_texture.cc(56)] Check failed: state_ == AttachmentState::kAttached.
D/CameraPreview(10851): resume()
D/CameraInstance(10851): Opening camera
D/CompatibilityChangeReporter(10851): Compat change id reported: 250678880; UID 10188; state: ENABLED
I/PlatformViewsController(10851): Hosting view in view hierarchy for platform view: 1
E/EGL_emulation(10851): eglQueryContext 32c0 EGL_BAD_ATTRIBUTE
E/EGL_emulation(10851): tid 10851: eglQueryContext(2128): error 0x3004 (EGL_BAD_ATTRIBUTE)
I/CameraBase(10851): Connect camera (legacy API) - overrideToPortrait 1, forceSlowJpegMode 0
D/CameraPreview(10851): resume()
W/CameraPreview(10851): initCamera called twice
E/FrameEvents(10851): updateAcquireFence: Did not find frame.
Below are the pages where I got the error:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:yedek_parca/pages/scanner.dart';
class Home extends StatefulWidget {
final Uri url;
Home(this.url);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final GlobalKey webViewKey = GlobalKey();
InAppWebViewController? webViewController;
InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
// useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
javaScriptEnabled: true,
cacheEnabled: true,
supportZoom: false,
clearCache: false),
android: AndroidInAppWebViewOptions(useHybridComposition: true),
ios: IOSInAppWebViewOptions(
allowsInlineMediaPlayback: true, sharedCookiesEnabled: true));
late PullToRefreshController pullToRefreshController;
@override
void initState() {
super.initState();
pullToRefreshController = PullToRefreshController(
options: PullToRefreshOptions(
color: Colors.blue,
),
onRefresh: () async {
if (Platform.isAndroid) {
webViewController?.reload();
} else if (Platform.isIOS) {
webViewController?.loadUrl(
urlRequest: URLRequest(url: await webViewController?.getUrl()));
}
},
);
}
@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
floatingActionButtonLocation:
FloatingActionButtonLocation.centerFloat,
body: SafeArea(
child: InAppWebView(
key: webViewKey,
initialOptions: options,
pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) =>
webViewController = controller,
onConsoleMessage: (controller, consoleMessage) {
print("Console: ${consoleMessage.message}");
},
initialUrlRequest: URLRequest(url: widget.url),
onLoadStart: (_, url) async {
await askPermission(url);
}))),
onWillPop: () async {
bool canGoBack = (await webViewController?.canGoBack()) ?? false;
if (canGoBack) {
webViewController?.goBack();
return false;
} else {
return true;
}
});
}
Future<void> askPermission(Uri? url) async {
var status = await Permission.camera.status;
if (status.isDenied) {
// Kamera izni iste
await Permission.camera.request();
}
// Kullanıcının izin verip vermediğini kontrol et
if (await Permission.camera.isGranted) {
// Kamera erişimine izin verildi, burada istediğiniz işlemleri yapabilirsiniz.
// ignore: use_build_context_synchronously
if (url.toString() == "https://link") {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Scanner()));
}
}
}
}
and
class Scanner extends StatefulWidget {
// final Function(String value) onScanned;
// const Scanner({super.key});
@override
// ignore: library_private_types_in_public_api
_ScannerState createState() => _ScannerState();
}
class _ScannerState extends State<Scanner> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
bool canScan = true;
bool isCameraInitialized = false;
QRViewController? controller;
final resultUrl =
"https://link";
final items = [
NavigationModel(Icons.fullscreen, "TARA",
Uri.parse("https://link")),
NavigationModel(Icons.list, "PARÇA KODU",
Uri.parse("https://link")),
NavigationModel(Icons.list, "OEM KODU",
Uri.parse("https://link")),
NavigationModel(Icons.power_settings_new, "Geri",
Uri.parse("https://link"))
];
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton.extended(
label: const Text("Flaş"),
onPressed: () => controller?.toggleFlash()),
body: QRView(
key: qrKey,
onQRViewCreated: onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.white,
borderRadius: 30,
borderLength: 60,
borderWidth: 5)),
bottomNavigationBar: BottomNavigationBar(
items: items
.map((x) =>
BottomNavigationBarItem(icon: Icon(x.icon), label: x.title))
.toList(),
onTap: (index) {
print(index);
try {
setState(() {
if (index != 0) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Home(items[index].url)));
}
});
} catch (e) {
print(e);
}
}),
);
}
void onQRViewCreated(QRViewController controller) {
this.controller = controller;
isCameraInitialized = true;
controller.scannedDataStream.listen((scanData) async {
if (canScan) {
if (scanData.code!.isNotEmpty) {
canScan = false;
final lst = await Requests.checkProduct(scanData.code!);
if (lst.isEmpty) {
showError();
} else {
this.controller?.dispose();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
Home(Uri.parse(resultUrl + scanData.code!)),
),
);
}
}
}
});
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
void showError() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Hata"),
content: Text("Ürün bulunamadı!"),
actions: [
TextButton(
child: Text("Tamam"),
onPressed: () {
canScan = true;
Navigator.pop(context);
})
],
));
}
}
please help me:(
When I open the camera for scan qr code, I get a fatal error and the phone shuts down. This does not happen on iOS. in Flutter
I believe you are using Flutter version 3.16.X, as there is an ongoing issue with that particular release Flutter#138947
If this is the case, you have two options:
Option 1
Downgrade Flutter to version 3.13.9:
Option 2
Switch to the Flutter master channel. However, please note that the master channel is not recommended for production apps:
In the future, when reporting issues with Flutter, it is crucial to specify the Flutter version you are using.