gpt4 book ai didi

android - Flutter 深度链接

转载 作者:行者123 更新时间:2023-12-04 23:40:51 24 4
gpt4 key购买 nike

根据 Flutter 官方 deep linking page ,我们不需要任何插件或原生 Android/iOS 代码来处理深层链接。
但它并没有真正告诉我们如何从该链接获取数据。我是从编码的角度说的。当然,他们在那里写道:
enter image description here
但这并没有告诉我在哪里 我应该写什么代码来实际获得完整的链接。我一直在寻找示例/教程,但我找不到任何不使用插件来处理深度链接的东西。
现在,我所做的只是添加 <intent-filter> AndroidManifest.xml 中的标签文件并单击链接,我的应用程序已开始显示。但我不知道如何从该链接中提取数据。
有没有人可以在这里指导我?提前致谢。

最佳答案

您需要特定于平台的代码来处理深度链接。如果您按照文档中的链接提及,您会找到完整的示例。

private val CHANNEL = "poc.deeplink.flutter.dev/channel"
private var startString: String? = null
override fun configureFlutterEngine(@NonNull flutterEngine:FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)

MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "initialLink") {
if (startString != null) {
result.success(startString)
}
}
}
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val intent = getIntent()
startString = intent.data?.toString()
}
flutter 代码:
class DeepLinkBloc extends Bloc {

//Event Channel creation
static const stream = const
EventChannel('poc.deeplink.flutter.dev/events');

//Method channel creation
static const platform = const
MethodChannel('poc.deeplink.flutter.dev/channel');

StreamController<String> _stateController = StreamController();

Stream<String> get state => _stateController.stream;

Sink<String> get stateSink => _stateController.sink;


//Adding the listener into contructor
DeepLinkBloc() {
//Checking application start by deep link
startUri().then(_onRedirected);
//Checking broadcast stream, if deep link was clicked in opened appication

stream.receiveBroadcastStream().listen((d) => _onRedirected(d));
}


_onRedirected(String uri) {
// Here can be any uri analysis, checking tokens etc, if it’s necessary
// Throw deep link URI into the BloC's stream
stateSink.add(uri);
}


@override
void dispose() {
_stateController.close();
}


Future<String> startUri() async {
try {
return platform.invokeMethod('initialLink');
} on PlatformException catch (e) {
return "Failed to Invoke: '${e.message}'.";
}
}
}
点击此链接了解更多详情。
https://medium.com/flutter-community/deep-links-and-flutter-applications-how-to-handle-them-properly-8c9865af9283

关于android - Flutter 深度链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68081448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com