If a mini program needs capabilities from the host app that the SDK doesn't support, developers can register custom APIs to enable these capabilities. This allows the mini program to call custom APIs provided by the host application.
Define a custom mini program API and associate it with an API handler. apiName: The name of the custom API. The mini program can use this API by calling wx.invokeNativePlugin().
apiHandler: The function that handles the API calls.
void registerMiniAppApi(String apiName, TcmppMiniAppApiHandler apiHandler)
Sample code:
final _tcmppFlutterPlugin = TcmppFlutter();
...
@override
void initState() {
super.initState();
...
_tcmppFlutterPlugin.registerMiniAppApi("myApiName",myApiHandler);
}
The mini program uses wx.invokeNativePlugin with api_name to call the Flutter-registered myApiName.
Sample code:
example code:
var opts = {
api_name: 'myApiName',
success: function(res) {
log(res);
},
fail: function(res) {
log(res);
},
complete: function(res) {
log(res);
},
data: {
name : 'kka',
age : 22
}
}
wx.invokeNativePlugin(opts);
Implement the myApiHandler function on the Flutter side:
Future<Map<String, dynamic>?> myApiHandler(MiniApiCall call) async {
print("API : ${call.apiName}");
print("AppInfo: ${call.appInfo}");
print("WebView ID: ${call.webViewId}");
print("params: ${call.params}");
return {"result": "success","method":"myApiHandler"};
}
Was this page helpful?