设备后退按钮不返回到以前的 webview

Device back button not returning to previous webview

刚接触flutter,我的项目app差不多好了。但不知为何,我无法回到之前的webview。它似乎没有检测到我的点击。

顺便说一句,我正在使用 Flutter 开发团队 WebView

我试过的方法:将 Scaffold 包装在 WillPopScope 中 - 不起作用

此外,我的 Future<bool> 有一个 return 语句,但它似乎没有检测到或读取那个 return 语句。

_exitApp 也是一条蓝色的波浪线,表示我应该有一个 return 语句,您可以在下面看到它,但由于某种原因它没有阅读它。

我的代码:

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';");
          },
          ),

请检查这个,它可能对你有帮助

 _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);
});

}

并从 onwillpop() 调用

_exitApp(context,_controller.future),