Run this code sample on Android
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: SafeArea(
child: WebView(
initialUrl: "https://www.w3schools.com/howto/howto_js_copy_clipboard.asp",
javascriptMode: JavascriptMode.unrestricted,
),
),
);
}
}
Click the "Copy Text" button
Expected results: The text should be copied to the clipboard.
Actual results: Log message: [INFO:CONSOLE(0)] "Uncaught (in promise) NotAllowedError: Write permission denied."
FYI:
- This issue does not occur on Flutter webview for iOS.
- This issue was reported to the Flutter here ( https://github.com/flutter/flutter/issues/111388 ) and here ( https://github.com/flutter/flutter/issues/133817 ), where they said they are not going to fix the bug/issue. However, they did say "add it in your own code by setting up a JavaScript channel for the webview, and implementing it via Flutter's Clipboard class," which we do not know how to do. They also asked us to post the issue in the community here.
- We noticed that this issue was reported here ( webview_flutter: Uncaught (in promise) NotAllowedError: Write permission denied ) but there was no solution provided there and we have more detail on the problem on this post.
We have tried many many experiments from the internet and from AI for this problem and none of it has worked so far or involved editing java code, which does not seem to apply in flutter context. For example:
- see stackoverflow issues in https://trello.com/c/ihQY0Wbt/525-webviewflutter-cant-copy-text-via-javascript-navigatorclipboardwritetext _ tried using the flutter permission_handler package but made no difference
AI is currently also suggesting this, but does not even seem to compile
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter/services.dart';
class WebViewExample extends StatefulWidget {
@override
_WebViewExampleState createState() => _WebViewExampleState();
}
class _WebViewExampleState extends State<WebViewExample> {
WebViewController _controller;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter WebView example'),
),
body: WebView(
initialUrl: 'https://flutter.dev',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
_controller.evaluateJavascript("window.addEventListener('flutterInAppWebViewPlatformReady', function(event) {window.flutter_inappwebview.callHandler('test', 'Text to copy').then(function(result) {console.log(result);});});");
},
javascriptChannels: <JavascriptChannel>[
JavascriptChannel(
name: 'Clipboard',
onMessageReceived: (JavascriptMessage message) {
Clipboard.setData(ClipboardData(text: message.message));
}),
].toSet(),
),
);
}
}
Any help would be greatly appreciated. Thanks!