前言
经过上一篇的博客,大家应该都清楚ios和android的插件开发的原理,这节课我们就来讲讲怎么参数传递的
准备工具
这套课程是采用Android Studio和xcode进行开发的。当前在此之前请准备好Flutter开发环境,我这里就不进行讲解了。
Android部分
其实Android的参数传递我之前有一篇博客已经写好了,大家直接去Android参数传递这篇博客看看,这里就不重复写了。
IOS部分
那么就直接进入我们这篇博客的主题IOS部分参数传递与回传
在flutter_plugin.dart添加一个方法sendMessage
static Future<String> sendMessage(String message) async { //接受main.dart的参数
//下面是把参数传递到底层
final String res = await _channel.invokeMethod('sendMessage',<String,dynamic>{'message':message});
return res; //接受底层的参数,并把参数返回到flutter层
}
到底层方法进行编写添加一个else-if语句
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
}else if([@"getNative" isEqualToString:call.method]){
result(@"Hello Flutter plugin");
}else if([@"sendMessage" isEqualToString:call.method]){
//接受传递的参数
NSDictionary* argsMap=call.arguments;
NSString * message=argsMap[@"message"];
//字符串拼接和输出
NSString * newMessage=[NSString stringWithFormat:@"%@%@",@"IOS端:",message];
NSLog(newMessage);
//返回参数
result(message);
}
else {
result(FlutterMethodNotImplemented);
}
在main.dart中写一个按钮,调用刚刚写的方法
RaisedButton(
child: Text(
'参数传递',
style: TextStyle(
fontSize: 20.0
),
),
onPressed: ()async{
//调用刚刚写的方法,并传递参数
String message=await FlutterPlugin.sendMessage("Flutter plugin");
print("Flutter:"+message);
},
)
今天就分享到这里了,因为oc才刚刚开始学习,写的不好,大家请见谅,大家一起学习啦