New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[webview_flutter] NavigationDecision.prevent not working on Android #68785
Comments
I can see it's not working on Android and on iOS, the initial page isn't loaded #65502 complete code sampleimport 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Completer<WebViewController> _controller = Completer<WebViewController>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.white),
title: Text(
'Webview test',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
backgroundColor: Color.fromRGBO(56, 64, 82, 1),
),
body: WebView(
initialUrl: 'https://flutter.dev',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController controller) {
_controller.complete(controller);
},
navigationDelegate: (NavigationRequest _redirectRequest) {
return NavigationDecision
.prevent; //Blocks all internal html navigation requests.
},
),
);
}
}
flutter doctor -v[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.7 19H2, locale en-GB)
• Flutter version 1.22.2 at /Users/tahatesser/Code/flutter_stable
• Framework revision 84f3d28555 (7 days ago), 2020-10-15 16:26:19 -0700
• Engine revision b8752bbfff
• Dart version 2.10.2
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Users/tahatesser/Code/sdk
• Platform android-30, build-tools 30.0.2
• ANDROID_HOME = /Users/tahatesser/Code/sdk
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.1, Build version 12A7403
• CocoaPods version 1.10.0.rc.1
[!] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.50.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.15.1
[✓] Connected device (1 available)
• RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29)
! Doctor found issues in 1 category.
There is a deep investigation on this issue in #65502 (comment) |
@intenseandnoble There is a behavior difference between android and iOS. on Android, the first url (in your case, the main.URL) is always loaded. Is there any reason that you explicitly ask the webview to load a url and then you want to prevent it from loading? |
The loading of the initial url is not the issue why I am here. I gave a simplified example. The main issue is the one I wanted to test (which I refered to briefly in my first comment). It is that I want to evaluate a string of Javascript at each navigation. Unable to do so, I tried preventing navigation and creating a new webview with said javascript, after the initial url has been loaded. It did not work. Uncertain whether it was my code or a bug, I tested, found this bug and created a StackOverflow question about my actual issue. |
Are there any updates about this? |
I got it to work in a roundabout way. Through trial and error, I came to conclude that evaluateJavascript is evaluated once but always kept. So my solution was to put the js code in an interval. Due to the nature of the application, it seems a satisfying solution. (The application is mainly used to find documents, so it is a "search, download file and close app" situation) *thanks for asking about an update, it gives me an opportunity to give back to the community |
Reproducible with flutter doctor -v
|
You can try to load some dummy URL inside prevent, that should stop the url from changing. Temp workaround |
Hi, this is my setup
As the title says, I am trying to prevent navigation. In my current case, I want to prevent all navigation. It is to test another issue.
This following sample does not prevent navigation.
The text was updated successfully, but these errors were encountered: