问题描述
我正在尝试使用Flutter构建应用程序,我想知道如何通过Flutter检索设备的代理设置.
可以假定HttpClient不会自动选择它,但是您可能需要对其进行测试.
因此,现在您需要使用插件与本机代码进行交互.已经有丰富的库插件可以提供从电池电量到视频播放器的所有内容.我在任何地方都看不到代理,因此您需要编写自己的插件(这只是标准的flutter项目类型之一:应用程序,程序包(仅允许使用Dart代码)和插件).插件有点像一个包(其他项目可以依赖它),但也包含本机代码.它还包括一个微型应用程序,因此您可以在开发插件代码时对其进行测试.
您的插件最终将类似于现有的Connectivity插件,因此您可能要从那里复制.在您的Android方法实现中,您将替换
NetworkInfo info = manager.getActiveNetworkInfo();
使用
ProxyInfo defaultProxy = manager.getDefaultProxy();
您已经返回了两个值,即主机名和端口,因此请将它们放在Map中
Map<String, String> map = new HashMap<String, String>();
map.put("host", defaultProxy.getHost());
map.put("port", Integer.toString(defaultProxy.getPort()));
result.success(map);
如果将更改提交给Connectivity插件,则奖励积分.
I am trying to build an app using flutter and I would like to know how to retrieve the proxy settings of the device through flutter.
It's probably safe to assume that the HttpClient doesn't pick this up automatically, but you might want to test that.
So, now you need to interact with native code using plugins. There is already a rich library of plugins providing everything from battery level to video player. I can't see proxy in there anywhere, so you need to write your own plugin (which is just one of the standard flutter project types: app, package (just Dart code allowed) and plugin). A plugin is a bit like a package (other projects can depend on it) but includes native code too. It also includes a mini-app so that you can test your plugin code while developing it.
Your plugin will end up being similar to the existing Connectivity plugin, so you may want to copy from there. In your Android method implementation you will replace
NetworkInfo info = manager.getActiveNetworkInfo();
with
ProxyInfo defaultProxy = manager.getDefaultProxy();
You have return two values, the host name and the port, so put these in a Map
Map<String, String> map = new HashMap<String, String>();
map.put("host", defaultProxy.getHost());
map.put("port", Integer.toString(defaultProxy.getPort()));
result.success(map);
Bonus points if you submit your changes to the Connectivity plugin.
这篇关于如何使用flutter获取android/ios设备的代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!