34 lines
967 B
Dart
34 lines
967 B
Dart
|
import 'dart:convert';
|
||
|
import 'dart:io' show Platform;
|
||
|
|
||
|
import 'package:flutter/foundation.dart';
|
||
|
import 'package:webview_flutter/webview_flutter.dart';
|
||
|
|
||
|
/// Evaluates a JavaScript function on the given WebView.
|
||
|
///
|
||
|
/// The JavaScript function must return a String.
|
||
|
///
|
||
|
/// On Android, the `String` resulted from the evaluation
|
||
|
/// is JSON parsed. On iOS, the `String` is returned as is.
|
||
|
///
|
||
|
/// Other platforms are not supported. The returned value
|
||
|
/// in this case will be `null`.
|
||
|
Future<String> wInvoke({
|
||
|
@required WebViewController webViewController,
|
||
|
@required String jsFunctionContent,
|
||
|
bool isFunctionAlready = false
|
||
|
}) async {
|
||
|
final actualJS = isFunctionAlready ?
|
||
|
jsFunctionContent :
|
||
|
"""
|
||
|
(() => {
|
||
|
$jsFunctionContent
|
||
|
})()
|
||
|
""";
|
||
|
|
||
|
final res = await webViewController.evaluateJavascript(actualJS);
|
||
|
|
||
|
if (Platform.isAndroid) return JsonDecoder().convert(res) as String;
|
||
|
else if (Platform.isIOS) return res;
|
||
|
else return null;
|
||
|
}
|