flutter - 从 dart 文件颤振中的本机代码重定向问题回调后
问题描述
我使用 MethodChannel 完成了颤振与本机代码之间的通信。它在颤振到本机之间的桥梁已经完成,但是当我尝试将本机屏幕重定向到颤振屏幕时,它不会重定向。我正在使用 Navigator 推送方法来重定向屏幕。请检查以下代码:
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() 中不重定向。
解决方案
尝试以这种方式使用它,Using then
Statement
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()),
);
});
如果这也不起作用,并且您在 dispose 之后调用了与 setStateerror
相关的任何内容,那么试试这个,
await platform.invokeMethod('showNativeView', {"text": "Maulik"}).then((onValue) {
if (this.mounted) {
setState(() {
// Your Navigation Code Here
});
}
});
推荐阅读
- r - 一个 R 包未成功安装时如何退出 R 脚本?
- jsf - Oracle ADF 在查询组件中添加secondaryPattern to Date
- python - 覆盆子与 Python
- ember.js - 从其他模型 Ember 获取数据
- javascript - Laravel 出现条纹元素错误:请确保您尝试使用的元素仍然已安装
- python - 我的问题是关于子域搜索。我想知道查找网站子域的实用方法
- c++ - 如何将字符串添加到包含 c++ 中的字符串和整数的结构类型数组?
- django - 当覆盖“perform_destroy”时得到 {“detail”:“Method \"POST\" not allowed."}
- java - 未来日期/年份的 Java 夏令时 (TimeZone/ZoneId) 支持
- rest - Azure DevOps/VSTS REST API 不获取变更集的更改