-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not Getting callbacks from javascriptHandler in android #2431
Comments
Do you call the callback correctly on JavaScript side? How do you call it? |
This is how i call on the web side
|
Ok, please post some logs. Also, you mentioned Android but not the Android version. Please, add that info. |
Android 33 and im getting a console log in this. But not on the callback
|
Are you sure you are reaching that specific code? There is no console log about |
the code is reaching there.. and the information im passing through reaches the db.. This is not triggering on the flutter inappwebview. I've tried this with webview_flutter and im getting the response back through callback. But for this case its not callback is not triggering. And this is the correct way to invoke handler right? from web ?
is there any issues on this?! |
Do you mean that |
no this part. is this correct? Anything i missed? window.flutter_inappwebview.callHandler( |
It is correct. Have you tried to inspect with Chrome Developer Tools for remote debugging? Do you see any JavaScript error there? could you also post your full InAppWebView code implementation? |
Here's the implemention of inappwebview
|
I'm not able to reproduce the issue. There is something that you are not doing correctly but I don't know, probably on JavaScript side. Here is a simple test (using your settings or not didn't change anything to this test): import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
}
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey webViewKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Test")),
body: SafeArea(
child: Column(children: <Widget>[
Expanded(
child: Stack(
children: [
InAppWebView(
key: webViewKey,
initialData: InAppWebViewInitialData(data: """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="test">TEST</button>
<script>
document.querySelector("#test").addEventListener("click", function() {
const data = {
foo: "bar",
arr: [1, true, 3],
obj: {a: 1, b: false, c: 3},
};
window.flutter_inappwebview.callHandler(
'ResponseChannel',
data
);
});
</script>
</body>
</html>
"""),
onWebViewCreated: (controller) {
controller.addJavaScriptHandler(
handlerName: 'ResponseChannel',
callback: (arguments) {
debugPrint("Data received from Angular : $arguments");
if (arguments.isNotEmpty) {
debugPrint(
"Data received from Angular: ${arguments[0]}");
} else {
debugPrint("No data received from Angular");
}
},
);
},
onConsoleMessage: (_, consoleMessage) async {
if (kDebugMode) {
print(consoleMessage.message);
}
},
onLoadStop: (controller, url) async {
// just for test
controller.evaluateJavascript(source: """document.querySelector("#test").click();""");
await Future.delayed(const Duration(seconds: 2));
// you can try also this
controller.evaluateJavascript(source: """
const data = {
foo: "bar",
arr: [1, true, 3],
obj: {a: 1, b: false, c: 3},
};
window.flutter_inappwebview.callHandler(
'ResponseChannel',
data
);
""");
},
)
],
),
),
])));
}
} Logs:
|
I am experiencing the same issue, seems like on the web side, window.isFlutterInAppWebViewReady is not being called |
@dawid-niedzwiecki You can listen for the On Android, if the WebView supports the feature If you MUST use for whatever reason the // execute inside the "flutterInAppWebViewPlatformReady" event listener
window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
const args = [1, true, ['bar', 5], {foo: 'baz'}];
window.flutter_inappwebview.callHandler('myHandlerName', ...args);
});
// or using a global flag variable
var isFlutterInAppWebViewReady = false;
window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
isFlutterInAppWebViewReady = true;
});
// then, somewhere in your code
if (isFlutterInAppWebViewReady) {
const args = [1, true, ['bar', 5], {foo: 'baz'}];
window.flutter_inappwebview.callHandler('myHandlerName', ...args);
} However, you can take my example of the previous comment and update it to your needs. |
Is there an existing issue for this?
Current Behavior
Im using inappwebview communication with javascript handler. But The handler is not getting callbacks.
Expected Behavior
Expecting to get the value from web to flutter.
Steps with code example to reproduce
Code of javascript handler used
Stacktrace/Logs
Stacktrace/Logs
Flutter version
v3.24.5
Operating System, Device-specific and/or Tool
Android (Emulator)
Plugin version
v6.1.5
Additional information
I was able to find this reading documentation. Is this the correct method? When using webiew_flutter im able to get the result im expected .
Self grab
The text was updated successfully, but these errors were encountered: