How to configure go back button in Browser for Flutter Web App

9,311

Solution 1

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

Solution 2

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()
              ],
            ),
          ),
        ),
      ),
    );
  }

Solution 3

onWillPop: () {
  Navigator.pop(context);
  return new Future(() => true);
}
Share:
9,311
Faslur Rajah
Author by

Faslur Rajah

Updated on December 19, 2022

Comments

  • Faslur Rajah
    Faslur Rajah over 1 year

    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)
    
  • Dean Villamia
    Dean Villamia almost 4 years
    Thanks! Went mad trying to find a solution for this.
  • Al Mamun
    Al Mamun over 2 years
    Glad to know that it works for you!