如何解决在 dart 文件颤振中从本机代码重定向问题回调之后
我已经使用 MethodChannel 完成了 Flutter 与本机代码之间的通信。它在颤振到本机完成之间的桥梁,但是当我尝试将本机屏幕重定向到颤振屏幕时,它没有重定向。我正在使用导航器推送方法来重定向屏幕。请检查以下代码:
class MyHomePage extends StatelessWidget {
BuildContext mcontext;
static const platform = const MethodChannel(
'flutter.rortega.com.basicchannelcommunication');
final String title;
MyHomePage({Key key,this.title}) : super(key: key) {
platform.setMethodCallHandler(_handleMethod);
}
@override
Widget build(BuildContext context) {
mcontext = context;
return new Scaffold(
appBar: new AppBar(
title: new Text(title),),body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
new RaisedButton(
child: new Text('Show native view'),onPressed: _showNativeView,],);
}
Future<Null> _showNativeView() async {
await platform.invokeMethod('showNativeView',{"text": "Maulik"});
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "message":
String alice = call.arguments['message'];
print(alice);
pushPreviewScreen(mcontext);
}
}
pushPreviewScreen(BuildContext mcontext) {
print("calledFunction::");
Navigator.push(
mcontext,MaterialPageRoute(builder: (context) => SecondScreen()),);
}
}
此处“CalledFunction::”在控制台中打印但不在SecondScreen()中重定向。
解决方法
尝试这样使用它,使用 then
语句
await platform.invokeMethod('showNativeView',{"text": "Maulik"}).then((onValue) {
//you can also check the returned `value` from native `code` and return `true` or `false` from the `native` `code`,and on the basis of that you can send the user to the screen like `if(value)` //send to home screen else //send back to screen
Navigator.push(
mcontext,MaterialPageRoute(builder: (context) => SecondScreen()),);
});
如果那也不起作用,并且您得到任何与 setState 相关的 error
在 dispose 之后调用,那么试试这个,
await platform.invokeMethod('showNativeView',{"text": "Maulik"}).then((onValue) {
if (this.mounted) {
setState(() {
// Your Navigation Code Here
});
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。