21

I'm not asking about webview. This is about Flutter web app. I need to go back to a specific page when user press back button which is inbuilt in browser.

enter image description here

Any guessing ?

I'm getting this error when I press back button

Error: Assertion failed: org-dartlang- 
    sdk:///flutter_web_sdk/lib/_engine/engine/history.dart:110:14 
    _userProvidedRouteName != null
    is not true
        at Object.throw_ [as throw] (http://localhost:8300/dart_sdk.js:4770:11)
        at Object.assertFailed (http://localhost:8300/dart_sdk.js:4721:15)
Faslur Rajah
  • 861
  • 2
  • 10
  • 17
  • have you tried `WillPopScope()` ? https://stackoverflow.com/questions/45916658/how-to-deactivate-or-override-the-android-back-button-in-flutter – FloW Apr 20 '20 at 12:18
  • https://stackoverflow.com/questions/59763779/flutter-web-how-to-disable-backward-button-of-browser-in-flutter-web-applicatio – FloW Apr 20 '20 at 12:22
  • I'm getting this error – Faslur Rajah Apr 20 '20 at 13:25
  • Error: Assertion failed: org-dartlang-sdk:///flutter_web_sdk/lib/_engine/engine/history.dart:110:14 _userProvidedRouteName != null is not true at Object.throw_ [as throw] (http://localhost:8300/dart_sdk.js:4770:11) at Object.assertFailed (http://localhost:8300/dart_sdk.js:4721:15) – Faslur Rajah Apr 20 '20 at 13:25
  • please share your code, if its not working :) – FloW Apr 20 '20 at 13:58
  • 1
    Please provide the solution as well. if it is working @FaslurRajah – Shikhar Singh Feb 09 '21 at 07:14

5 Answers5

15

In case if you don't want to navigate to a new page

    @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => true,
      child: Scaffold(

        key: _scaffold,
        backgroundColor: Colors.indigo,
        body: Center(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                registerForm(),
                registerBtn(),
                SizedBox(height: 30,),
                _buildSignInButton()
              ],
            ),
          ),
        ),
      ),
    );
  }
Al Mamun
  • 630
  • 2
  • 8
  • 21
  • Any idea why it doesn't work for [Rony](https://stackoverflow.com/q/67325662/3935156) and [Umut](https://stackoverflow.com/q/70125238/3935156), as well as myself? – BeniaminoBaggins Jul 26 '22 at 20:17
4
onWillPop: () {
  Navigator.pop(context);
  return new Future(() => true);
}
Christer
  • 2,746
  • 3
  • 31
  • 50
3

onWillPop Navigate to a new Page

class webScope extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: () async => Navigator.push(context, MaterialPageRoute(builder: (context) => new NewPageWidget())),
      child: Scaffold(
        appBar: new AppBar(
          title: new Text("webScope"),
        ),
      ),
    );
  }
}
FloW
  • 1,211
  • 6
  • 13
0

If you are looking to emulate the browser back button from within a pushed route in your Flutter web app, I have achieved this via use of Router.neglect:

AppBar(
        leading: GestureDetector(
          child: const Icon(Icons.close_rounded, color: Colors.white),
          onTap: () => Router.neglect(context, () => context.pop()),
        ),
      ),
CoastalB
  • 695
  • 4
  • 15
-1

For disable this chrome back button you can use

 onWillPop: () {
  exit(0);
  return new Future(() => true);
}

and for hand back press for back page you can use willPop:

 WillPopScope(
  onWillPop: () async => Navigator.push(context, MaterialPageRoute(builder: (context) => YOUROLDPAGE()),
  child: Scaffold(
    appBar: new AppBar(
      title: new Text("Home Page"),
    ),
  ),
);
Alok Dubey
  • 44
  • 5
  • 3
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Apr 24 '22 at 04:18
  • 1
    Thank you Jeremy Caney for your feedback. Its my updated code please check. – Alok Dubey Jan 19 '23 at 09:36