我在Flutter中有一个webview和一个链接到http://mywebsite.com/dummy.pdf的网页
问题是,当用户点击pdf链接时,webview监听器不会监听。
flutterWebviewPlugin.onUrlChanged.listen((String url) async {
if (url.contains('.pdf')) {
print(url); //This print is never done
}
if (url.contains('mailto:')) {
print(url); //This print is ok
}
if (url.contains('txt')) {
print(url); //This print is ok
}
if (url.contains('foobar')) {
print(url); //This print is ok
}
}
如何解决这个问题?
更新1
这个问题与那些指向“非托管”文件的urls有关。
flutterWebviewPlugin.onUrlChanged.listen((String url) async {
if (url.contains('.pdf')) {
print(url); //This print is never done
}
if (url.contains('.doc')) {
print(url); //This print is never done
}
if (url.contains('mailto:')) {
print(url); //This print is ok
}
if (url.contains('.txt')) {
print(url); //This print is ok
}
if (url.contains('.mp3')) {
print(url); //This print is ok
}
}
发布于 2020-07-01 16:54:30
我不知道这是不是更好的方法,但如果问题只与pdf
有关,那么我们只能通过Dart
编程方法来解决这个问题。
但是,我觉得您也必须检查一下这一点:url.contains('pdf')
,而不是在contains()
中使用.pdf
因此,现在的解决方法是执行以下操作:
1. Check the length of the url
2. Since the pdf would come at the last, so we need the last three items
3. Check if it is `pdf` only or not, like pdfString = Substring of the String of last three chars // In this way you get the last three elements of the url
4. Do a check whether pdfString == 'pdf', if yes, print('OK') else // do something
最终解决方案
// This is a workaround for pdf only, so writing the code for that only
flutterWebviewPlugin.onUrlChanged.listen((String url) async {
// technically this should give you the last three chars, which is pdf in this case
var pdfString = url.substring(url.length-3);
// now checking if the pdfString contains that pdf or not
if(pdfString == 'pdf'){
//print your url
print(url); // OUTPUT WILL BE http://mywebsite.com/dummy.pdf
}
}
我希望,您会发现这足够有用,可以将其合并到代码中。如果pdf的url总是以.pdf结尾,这是一种可行的解决方案,。让我知道这是给你的,直到那时快乐的学习:)
https://stackoverflow.com/questions/62666741
复制相似问题