Flutter私有庫

Nem發表於2019-12-12

建立plugin

step1:建立plugin

flutter create --template=plugin xxx_network
複製程式碼

step2:編寫程式碼

# xxx_network.podspec 新增依賴的私有庫
s.dependency 'XXXNetwork'

# /xxx_network/example/ios/Podfile中新增私有索引源(Runner工程)
source 'https://github.com/CocoaPods/Specs.git'
source 'http://xxx.com/specs.git'

# pod install下,把依賴的XXXNetwork裝好
複製程式碼

開啟Example/ios中的Runner工程,plugin模組預設建立了swift和OC兩個版本的程式碼,我們用OC開發,這裡直接把SwiftFlutterXXXNetworkPlugin.swift檔案刪掉,把MethodChannel的建立和回撥方法寫在FlutterXXXNetworkPlugin.m中

@implementation FlutterXXXNetworkPlugin
// ?兩個都是FlutterPlugin協議的方法
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"xxx_network" binaryMessenger:[registrar messenger]];
    FlutterXXXNetworkPlugin *instance = [FlutterXXXNetworkPlugin new];
    [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    result(@"傳遞msg回撥給Flutter");
}
@end
複製程式碼

釋出plugin

方式一:上傳plugin到gitlab上

文件地址:dart.dev/tools/pub/d…

使用:

# pubspec.ymal
dependencies:
   xxx_network:
    git: http://xxx.com/xxx_network.git
複製程式碼

⚠️:因為xxx_network這個plugin依賴了私有的cocoapods庫,需要在Runner工程Podfile里加上私有索引源

# Runner的Podfile,在頂部指明私有索引源地址
source 'http://xxx/specs.git' 
複製程式碼

方式二:上傳plugin到私有pub server

地址:github.com/dart-lang/p…

step1:搭建pub server

官方提供了一個私有pub server的庫,按照文件在本機搭建一下

~ $ git clone https://github.com/dart-lang/pub_server.git
~ $ cd pub_server
~/pub_server $ pub get
...
~/pub_server $ dart example/example.dart -d /tmp/package-db
Listening on http://localhost:8080

To make the pub client use this repository configure your shell via:

    $ export PUB_HOSTED_URL=http://localhost:8080
複製程式碼

⚠️:執行dart命令的時候如果提示 dart: command not found,就是說沒有配置好Dart的環境變數。配置方法:開啟 ~/.bash_profile檔案,新增以下文字到末尾(路徑根據你的實際情況填寫)。

export PATH=${PATH}:/xxx/flutter/bin/cache/dart-sdk/bin
複製程式碼

儲存好後執行命令重新整理以下,重啟terminal就可以使用dart指令啟動本地的pub server了

source ~/.bash_profile
複製程式碼

step2:上傳plugin,在工程中使用

~/xxx_network $ export PUB_HOSTED_URL=http://localhost:8080
~/xxx_network $ pub get
...
~/xxx_network $ pub publish
Publishing xxx_network 0.0.1 to http://localhost:8080:
|-- ...
'-- pubspec.yaml

Package has 1 warning. Upload anyway (y/n)? y
Pub needs your authorization to upload packages on your behalf.
In a web browser, go to https://accounts.google.com/o/oauth2/auth?access_xxx.email
Then click "Allow access".

Waiting for your authorization...
Authorization received, processing...
Successfully uploaded package.
複製程式碼

⚠️:上傳過程中需要翻牆驗證一個地址(?例子是:accounts.google.com/o/oauth2/au…

相關文章