KDC
KDC

Reputation: 611

Device back button not returning to previous webview

New to flutter here, my project app is almost good now. But for some reason, I cant go back to the previous webview. It seems it's not detecting my tap.

Btw, Im using Flutter dev team WebView

What I've tried: Wrap Scaffold in WillPopScope-doesnt work

Also, my Future<bool> has a return statement but it seems doesnt detect or read that return statement.

The _exitApp also as a blue squiggly line which says I should have a return statement, below you can see it but for some reason its not reading it.

my code:

class HomePage extends StatefulWidget {
  // HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();


@override
void initState() {
  super.initState();
  firebaseCloudMessagingListeners();
}
void firebaseCloudMessagingListeners() {
  if (Platform.isIOS) iOSPermission();

  _firebaseMessaging.getToken().then((token){
    print(token);
  });

  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      setState(() {
      print("${message['data']['url']}");
      Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => NotificationClicked()));
      });
    },
    onResume: (Map<String, dynamic> message) async {
      print("${message['data']['url']}");
    },
    onLaunch: (Map<String, dynamic> message) async {  
      print("${message['data']['url']}");
    },
  );
}

void iOSPermission() {
  _firebaseMessaging.requestNotificationPermissions(
      IosNotificationSettings(sound: true, badge: true, alert: true)
  );
  _firebaseMessaging.onIosSettingsRegistered
      .listen((IosNotificationSettings settings)
  {
    print("Settings registered: $settings");
  });
}

  WebViewController myController;
      final Completer<WebViewController> _controller =
      Completer<WebViewController>();


/* The _exitApp also as a blue squiggly line which says I should have a return statement, below you can see it but for some reason its not reading it. */

Future<bool> _exitApp(BuildContext context) async {
  if (await myController.canGoBack()) {
    print("onwill goback");
    myController.goBack();
  } else {
    Scaffold.of(context).showSnackBar(
      const SnackBar(content: Text("No back history item")),
    );
    return Future.value(false);
  }
}

  @override
  Widget build(BuildContext context) {
    return SafeArea(
            child: WillPopScope(
              onWillPop: () => _exitApp(context),
                          child: Scaffold(
                    body: WebView(
                    initialUrl: 'https://syncshop.online/en/',
                    javascriptMode: JavascriptMode.unrestricted,
                    onWebViewCreated: (controller) {
                    _controller.complete(controller);
                  },
          onPageFinished: (controller) async {
          (await _controller.future).evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';");
              (await _controller.future).evaluateJavascript("document.getElementById('st_notification_1').style.display='none';");
              (await _controller.future).evaluateJavascript("document.getElementById('sidebar_box').style.display='none';");
          },
          ),

Upvotes: 0

Views: 727

Answers (1)

Avinash
Avinash

Reputation: 897

Please check with this, it may helps you

 _exitApp(BuildContext context,Future<WebViewController> controller) async {
controller.then((data) async {
  WebViewController controller= data;
 var goback= await controller.canGoBack();
  if (goback==true) {
    // ignore: missing_return
    print("onwill goback");
    controller.goBack();
  // ignore: missing_return
  } else {
    print("onwill not goback");
   Navigator.pop(context);
  }

}, onError: (e) {
  print(e);
});

}

and callfrom onwillpop()

_exitApp(context,_controller.future),

Upvotes: 2

Related Questions