0 purchases
spi flutter package
spi_flutter_package #
a dev tools create platform's code when use Platform channel exchange data
中文文档 #
Getting Started #
first, install it in your flutter project,
recommended you add this tools in dev_dependencies, because it's a dev tools, so you don't need publish
with your project sourcecode:
dev_dependencies:
spi_flutter_package:
path: version
copied to clipboard
and, in your flutter project, create custom dir: lib/channel, the dir use save your flutter channel dart code:
|____main.dart
|____channel //your flutter channel
| |____flutter2native //flutter invoke native dir
| | |____account.dart //your interface code
| |____native2flutter //native invoke flutter dir
| | |____flutter_fps.dart // your interface code
copied to clipboard
create account.dart:
abstract class IAccount{
Future<bool> login(String userName,String password);
void logout();
Future<String> getName();
Future<int> getAge();
}
copied to clipboard
more code: example
in flutter project test dir,open any dart file and write:
import 'package:spi_flutter_package/spi_flutter_package.dart';
import 'package:spi_flutter_package/file_config.dart';
void main() async {
await spiFlutterPackageStart([
FlutterPlatformConfig()
..sourceCodePath = "./lib/channel" //flutter source code
..channelName = "com.siyehua.spiexample.channel" //channel name
,
AndroidPlatformConfig()
..savePath = "./android/app/src/main/kotlin" //android save path
,
IosPlatformConfig()
..iosProjectPrefix = "MQQFlutterGen_" //iOS pre
..savePath = "./ios/Classes" //iOS save path
,
], nullSafe: true);
}
copied to clipboard
click left icon: ▶️, run main(), and you will find the generated code in your custom path. the more
info, you can checkout generated code.
when code auto create finish, you can add impl in platforms, eg: Android:
public class AccountImpl implements IAccount {
//....
@Override
public void logout() {
Log.e("android", "logout method, nothing should call back");
}
@Override
public void getName(ChannelManager.Result<String> callback) {
Log.e("android", "getName method");
callback.success("siyehua");
}
@Override
public void getAge(ChannelManager.Result<Long> callback) {
Log.e("android", "getAge method");
callback.success(1L);
}
}
copied to clipboard
Now, you can use this tools exchange data
in Android project:
//1. init(only first)
```java
ChannelManager.init(flutterEngine.getDartExecutor(), new ChannelManager.JsonParse() {
@Nullable
@Override
public String toJSONString(@Nullable Object object) {
//your json parse
return JSON.toJSONString(object);
}
@Nullable
@Override
public <T> T parseObject(@Nullable String text, @NonNull Class<T> clazz) {
//your json parse
return JSON.parseObject(text, clazz);
}
});
copied to clipboard
//2. add your impl class in ChannelManager
ChannelManager.addChannelImpl(IAccount.class, new AccountImpl());
<br>in flutter project:
```dart
//1. init(only first)
ChannelManager.init();
//2. invoke
IAccount account = ChannelManager.getChannel(IAccount);
var result = await account.login("userName", "password");
print(result);
account.logout();
var name = await account.getName();
print(name);
var age = await account.getAge();
print(age);
copied to clipboard
now, run your project and check result.
more info, you can check example
generated code #
in flutter project, the code will save in flutterPath/generated :
|____main.dart
|____channel
| |____generated //auto create
| | |____channel
| | | |____impl
| | | | |____iaccount_impl.dart
| | | |____parse
| | | | |____object_parse.dart
| | | |____ChannelManager.dart
| |____flutter2native //flutter invoke native
| | |____account.dart
| |____native2flutter //native invoke flutter
| | |____flutter_fps.dart
copied to clipboard
yet, you can find the auto create code in Android project, in androidSavePath + packageName dir:
.
|____com
| |____siyehua
| | |____spiexample
| | | |____MainActivity2.java
| | | |____AccountImpl.java
| | | |____MainActivity.kt
| | | |____channel //auto create
| | | | |____flutter2native
| | | | | |____IAccount.java
| | | | |____native2flutter
| | | | | |____Fps2.java
| | | | | |____FpsImpl.java
| | | | | |____Fps2Impl.java
| | | | | |____Fps.java
| | | | |____ChannelManager.java
copied to clipboard
Note: auto create code should not be edited
Proguard #
Note: if you open proguard, please add code in proguard-rules.pro file:
# in example, the package name is "com.siyehua.spiexample.channel"
-keep class {your android package name}.** {*;}
copied to clipboard
Other #
support: platforms_source_gen #
all method must return Future or void #
because Flutter Platform Channel must return Future
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.