Flutter 插件开发流程和问题分析

本文使用Android 来进行插件演示和问题分析,如果需要知道相关命令,请阅读文章结尾出的参考资料

1. Package 介绍

package是Flutter为了模块化开发和共享代码的一种代码组织方式,例如安卓中的一个jar包/AAR/maven中的一个库等。但是也有特别之处
package分为:

a.纯Dart包,不涉及原生部分
b.插件包,原生代码和Dart代码组合而成,可以实现一些特定平台的功能

他们可以被发布到pub.dev上,提供给他人使用

2.创建插件包

image.png

3.插件代码定义方分析

a.插件实现端之安卓
image.png
b.插件实现端之Dart

配置

name: flutter_plugin5  指定来插件的名称
description: A new Flutter plugin.
version: 0.0.1
homepage:

environment:
  sdk: ">=2.7.0 <3.0.0"
  flutter: ">=1.20.0"

dependencies:
  flutter:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter
flutter:
  plugin:
    platforms:
      android:
        package: com.kidswant.flutter_plugin5  指定的实现端的包名
        pluginClass: FlutterPlugin5Plugin 指定的实现端的类名
      ios:
        pluginClass: FlutterPlugin5Plugin

接口定义


import 'dart:async';

import 'package:flutter/services.dart';

class FlutterPlugin5 {
  static const MethodChannel _channel =
      const MethodChannel('flutter_plugin5');

  static Future get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
}

4.插件代码使用方分析

配置和使用

 flutter_plugin5:
    # When depending on this package from a real application you should use:
    #   flutter_plugin5: ^x.y.z
    # See https://dart.dev/tools/pub/dependencies#version-constraints
    # The example app is bundled with the plugin so we use a path dependency on
    # the parent directory to use the current plugin's version.
    path: ../

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_plugin5/flutter_plugin5.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await FlutterPlugin5.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}

使用方安卓端可能是自动生成的

安卓端

自动生成GeneratedPuginRegistrant类,内容是Engine注入插件


image.png
source:关联了dart代码
image.png

问题

a.发现问题

Can't resolve symbol io.flutter.plugin dependency


image.png
a.google搜索问题
image.png
c.问题issues

issues: https://github.com/flutter/flutter/issues/19830

d.阅读后我取的最好答案

I had the same problem but then I found out that the flutter plugin package is recognised when you open the example/android folder in Android studio. Now I can build the project and I can unittest my plugin from Android studio without adding the flutter.jar.
What I did step for step:
delete the .idea folder in the project (do not know if is needed but just to be sure)
in Android Studio click on 'Open an existing Android studio project' and select the folder your_plugin/example/android
If you want to build your library through gradle you can run ./gradlew :your_plugin:assembleDebug

e.白话说明

简单来说就是直接打卡安卓使用端工程是可以的,实验确实如此,所有可以这么用和编写插件即可

参考资料:https://flutterchina.club/developing-packages/

你可能感兴趣的:(Flutter 插件开发流程和问题分析)