go-flutter开发桌面应用(三) go-flutter插件中参数的传递 基本类型

1,123 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

前言: 今天刚看到,可以【搬运自己的原创文章】参加“程序员必备小知识”活动,十分激动。 想起来之前在某平台上还有几篇技术文章,觉着掘金技术氛围更浓厚,正想搬过来。

原文链接: go-flutter开发桌面应用(三) go-flutter插件中参数的传递 基本类型 - 知乎 (zhihu.com)

正文

本教程github代码

android和ios目录下的文件理论上不需要。

本文代码基于go-flutter开发桌面应用(二)的代码。

在go-flutter插件中添加新的函数

  • 新的函数名必须声明
  • 在初始化插件时,必须添加对应的响应函数
  • 添加具体的逻辑处理函数

hellogoflutter/go_plugin/hello/hello.go

package hello

import (
	"encoding/json"

	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
)

// go-flutter插件需要声明包名和函数名
// dart代码中调用时需要指定相应的包名和函数名
const (
	channelName = "bettersun.go-flutter.plugin.hello"
	hello       = "hello"
	message     = "message"
)

// 声明插件结构体
type HelloPlugin struct{}

// 指定为go-flutter插件
var _ flutter.Plugin = &HelloPlugin{}

// 初始化插件
func (HelloPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
	channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})
	channel.HandleFunc(hello, helloFunc)
	channel.HandleFunc(message, messageFunc)
	return nil
}

// 具体的逻辑处理函数,无参数传递
func helloFunc(arguments interface{}) (reply interface{}, err error) {

	return "Hello, go-flutter", nil
}

// 具体的逻辑处理函数,有参数传递
func messageFunc(arguments interface{}) (reply interface{}, err error) {

	var param string
	if arguments == nil {
		param = ""
	}

	switch arguments.(type) {
	case string:
		param = arguments.(string)
	default:
		newValue, _ := json.Marshal(arguments)
		param = string(newValue)
	}

	return "Welcome to go-flutter, " + param, nil
}

在go-flutter插件对应的Dart接口中添加函数的调用

注意有参和无参的调用方式。

hellogoflutter/lib/plugin/go/hello_plugin.dart

import 'dart:async';

import 'package:flutter/services.dart';

class HelloPlugin {
  // go-flutter插件中的包名,两者必须一致
  static const _channel = const MethodChannel("bettersun.go-flutter.plugin.hello");

  // go-flutter插件中的函数名,无参
  static Future<String> hello() async => _channel.invokeMethod("hello");

  // go-flutter插件中的函数名,有参
  static Future<String> message(String p) async => _channel.invokeMethod("message", p);
  
}

在确认画面中添加对应的方法调用

import 'package:flutter/material.dart';
import 'package:hellogoflutter/plugin/go/plugin.dart';

class HelloPage extends StatefulWidget {
  @override
  _HelloPageState createState() => _HelloPageState();
}

class _HelloPageState extends State<HelloPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hello'),
      ),
      body: Center(
          child: Column(
        children: <Widget>[
          FutureBuilder<String>(
            future: HelloPlugin.hello(),
            builder: (c, snapshot) {
              if (!snapshot.hasData) {
                return Text('Hello插件hello函数运行出错');
              }
              return Text(snapshot.data);
            },
          ),
          FutureBuilder<String>(
            future: HelloPlugin.message('warrior'),
            builder: (c, snapshot) {
              if (!snapshot.hasData) {
                return Text('Hello插件message函数运行出错');
              }
              return Text(snapshot.data);
            },
          ),
        ],
      )),
    );
  }
}

启动程序,跳转到确认画面的运行结果

image.png