All Products
Search
Document Center

Mobile Platform as a Service:Flutter project access guide

Last Updated:May 29, 2024

For more information about how to configure and initialize an mPaaS environment for a Flutter project, see Access a Flutter project.

Use mini program

  1. Register the mini program in the configureFlutterEngine of FlutterActivity.

    val messenger = flutterEngine.dartExecutor.binaryMessenger
    // Create a Channel object
    val channel = MethodChannel(messenger, "mpaas_mini_app")
    
    // Set a callback for the channel
    channel.setMethodCallHandler { call, res ->
        // Distribute different processing based on the method name
        when(call.method) {
    
            "mpaas_mini_app" -> {
                // Obtain the passed parameters
                val msg = call.argument<String>("msg")
                MPNebula.startApp(msg)
                // Notify that the execution is successful
                res.success("This is the result of the execution.")
            }
    
            else -> {
                // If an unrecognized method name is found, the execution fails.
                res.error("error_code", "error_message", null)
            }
        }
    }  
  2. Use mini program in Flutter.

    // Create a channel.
    const channel = const MethodChannel("mpaas_mini_app");
    
    
    Widget buttonView() {
      return  TextButton(
        child: Text("Open the mini program"),
        onPressed: () {
          callNativeMethod("2023072011111112");
        },
      );
    }
    
    void callNativeMethod(String msg) {
      try {
        // Call the native code method through the channel.
        Future future = channel.invokeMethod("mpaas_mini_app", {"msg": msg} );
        // Print the execution result.
        print(future.toString());
      } on PlatformException catch(e) {
        print(e.toString());
      }
    }