Can´t show snackbar in flutter login









up vote
0
down vote

favorite












Im creating a login form in flutter and I want to use snackbar to show a message when login fails. I read this documentation and if i have this code should works



final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
Scaffold.of(context).showSnackBar(snackBar);


But return this error




Scaffold.of() called with a context that does not contain a Scaffold.




My login.dart all code



import 'package:flutter/material.dart';
import 'package:fluttercrud/screens/home_page.dart';

class LoginPage extends StatelessWidget
static String tag = 'login-page';

@override
Widget build(BuildContext context)
final logo = Hero(
tag: 'hero',
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius: 48.0,
child: Image.asset('assets/logo.png'),
),
);

final email = TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
decoration: InputDecoration(
hintText: 'Usuario',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
);

final password = TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
hintText: 'Contraseña',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
);

final loginButton = Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Material(
borderRadius: BorderRadius.circular(30.0),
child: MaterialButton(
minWidth: 200.0,
height: 42.0,
onPressed: ()
final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
Scaffold.of(context).showSnackBar(snackBar);
//Navigator.of(context).pushNamed(HomePage.tag);
,
color: Colors.blue[300],
child: Text('Entrar', style: TextStyle(color: Colors.white)),
),
),
);

final forgotLabel = FlatButton(
child: Text(
'¿Contraseña olvidada?',
style: TextStyle(color: Colors.black),
),
onPressed: () ,
);

return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
logo,
SizedBox(height: 48.0),
email,
SizedBox(height: 8.0),
password,
SizedBox(height: 24.0),
loginButton,
forgotLabel
],
),
),
);




The scaffold return a error but i don´t know how can i fix this without rewrite all code.



So the question is: How can i to show the snackbar when login fails and avoid this error? And why this error appears?



UPDATED



 void initState() 
super.initState();
final snackBar = SnackBar(
content: Text(
'Usuario/Contraseña incorrecto',
textAlign: TextAlign.center,
));
Scaffold.of(context).showSnackBar(snackBar);
seriesList = _createSampleData();
animate = false;



And how can i show snackbar when init page?










share|improve this question



























    up vote
    0
    down vote

    favorite












    Im creating a login form in flutter and I want to use snackbar to show a message when login fails. I read this documentation and if i have this code should works



    final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
    Scaffold.of(context).showSnackBar(snackBar);


    But return this error




    Scaffold.of() called with a context that does not contain a Scaffold.




    My login.dart all code



    import 'package:flutter/material.dart';
    import 'package:fluttercrud/screens/home_page.dart';

    class LoginPage extends StatelessWidget
    static String tag = 'login-page';

    @override
    Widget build(BuildContext context)
    final logo = Hero(
    tag: 'hero',
    child: CircleAvatar(
    backgroundColor: Colors.transparent,
    radius: 48.0,
    child: Image.asset('assets/logo.png'),
    ),
    );

    final email = TextFormField(
    keyboardType: TextInputType.emailAddress,
    autofocus: false,
    decoration: InputDecoration(
    hintText: 'Usuario',
    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
    border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
    ),
    );

    final password = TextFormField(
    autofocus: false,
    obscureText: true,
    decoration: InputDecoration(
    hintText: 'Contraseña',
    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
    border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
    ),
    );

    final loginButton = Padding(
    padding: EdgeInsets.symmetric(vertical: 16.0),
    child: Material(
    borderRadius: BorderRadius.circular(30.0),
    child: MaterialButton(
    minWidth: 200.0,
    height: 42.0,
    onPressed: ()
    final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
    Scaffold.of(context).showSnackBar(snackBar);
    //Navigator.of(context).pushNamed(HomePage.tag);
    ,
    color: Colors.blue[300],
    child: Text('Entrar', style: TextStyle(color: Colors.white)),
    ),
    ),
    );

    final forgotLabel = FlatButton(
    child: Text(
    '¿Contraseña olvidada?',
    style: TextStyle(color: Colors.black),
    ),
    onPressed: () ,
    );

    return Scaffold(
    backgroundColor: Colors.white,
    body: Center(
    child: ListView(
    shrinkWrap: true,
    padding: EdgeInsets.only(left: 24.0, right: 24.0),
    children: <Widget>[
    logo,
    SizedBox(height: 48.0),
    email,
    SizedBox(height: 8.0),
    password,
    SizedBox(height: 24.0),
    loginButton,
    forgotLabel
    ],
    ),
    ),
    );




    The scaffold return a error but i don´t know how can i fix this without rewrite all code.



    So the question is: How can i to show the snackbar when login fails and avoid this error? And why this error appears?



    UPDATED



     void initState() 
    super.initState();
    final snackBar = SnackBar(
    content: Text(
    'Usuario/Contraseña incorrecto',
    textAlign: TextAlign.center,
    ));
    Scaffold.of(context).showSnackBar(snackBar);
    seriesList = _createSampleData();
    animate = false;



    And how can i show snackbar when init page?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Im creating a login form in flutter and I want to use snackbar to show a message when login fails. I read this documentation and if i have this code should works



      final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
      Scaffold.of(context).showSnackBar(snackBar);


      But return this error




      Scaffold.of() called with a context that does not contain a Scaffold.




      My login.dart all code



      import 'package:flutter/material.dart';
      import 'package:fluttercrud/screens/home_page.dart';

      class LoginPage extends StatelessWidget
      static String tag = 'login-page';

      @override
      Widget build(BuildContext context)
      final logo = Hero(
      tag: 'hero',
      child: CircleAvatar(
      backgroundColor: Colors.transparent,
      radius: 48.0,
      child: Image.asset('assets/logo.png'),
      ),
      );

      final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      decoration: InputDecoration(
      hintText: 'Usuario',
      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
      );

      final password = TextFormField(
      autofocus: false,
      obscureText: true,
      decoration: InputDecoration(
      hintText: 'Contraseña',
      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
      );

      final loginButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
      borderRadius: BorderRadius.circular(30.0),
      child: MaterialButton(
      minWidth: 200.0,
      height: 42.0,
      onPressed: ()
      final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
      Scaffold.of(context).showSnackBar(snackBar);
      //Navigator.of(context).pushNamed(HomePage.tag);
      ,
      color: Colors.blue[300],
      child: Text('Entrar', style: TextStyle(color: Colors.white)),
      ),
      ),
      );

      final forgotLabel = FlatButton(
      child: Text(
      '¿Contraseña olvidada?',
      style: TextStyle(color: Colors.black),
      ),
      onPressed: () ,
      );

      return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
      child: ListView(
      shrinkWrap: true,
      padding: EdgeInsets.only(left: 24.0, right: 24.0),
      children: <Widget>[
      logo,
      SizedBox(height: 48.0),
      email,
      SizedBox(height: 8.0),
      password,
      SizedBox(height: 24.0),
      loginButton,
      forgotLabel
      ],
      ),
      ),
      );




      The scaffold return a error but i don´t know how can i fix this without rewrite all code.



      So the question is: How can i to show the snackbar when login fails and avoid this error? And why this error appears?



      UPDATED



       void initState() 
      super.initState();
      final snackBar = SnackBar(
      content: Text(
      'Usuario/Contraseña incorrecto',
      textAlign: TextAlign.center,
      ));
      Scaffold.of(context).showSnackBar(snackBar);
      seriesList = _createSampleData();
      animate = false;



      And how can i show snackbar when init page?










      share|improve this question















      Im creating a login form in flutter and I want to use snackbar to show a message when login fails. I read this documentation and if i have this code should works



      final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
      Scaffold.of(context).showSnackBar(snackBar);


      But return this error




      Scaffold.of() called with a context that does not contain a Scaffold.




      My login.dart all code



      import 'package:flutter/material.dart';
      import 'package:fluttercrud/screens/home_page.dart';

      class LoginPage extends StatelessWidget
      static String tag = 'login-page';

      @override
      Widget build(BuildContext context)
      final logo = Hero(
      tag: 'hero',
      child: CircleAvatar(
      backgroundColor: Colors.transparent,
      radius: 48.0,
      child: Image.asset('assets/logo.png'),
      ),
      );

      final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      decoration: InputDecoration(
      hintText: 'Usuario',
      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
      );

      final password = TextFormField(
      autofocus: false,
      obscureText: true,
      decoration: InputDecoration(
      hintText: 'Contraseña',
      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
      );

      final loginButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
      borderRadius: BorderRadius.circular(30.0),
      child: MaterialButton(
      minWidth: 200.0,
      height: 42.0,
      onPressed: ()
      final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
      Scaffold.of(context).showSnackBar(snackBar);
      //Navigator.of(context).pushNamed(HomePage.tag);
      ,
      color: Colors.blue[300],
      child: Text('Entrar', style: TextStyle(color: Colors.white)),
      ),
      ),
      );

      final forgotLabel = FlatButton(
      child: Text(
      '¿Contraseña olvidada?',
      style: TextStyle(color: Colors.black),
      ),
      onPressed: () ,
      );

      return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
      child: ListView(
      shrinkWrap: true,
      padding: EdgeInsets.only(left: 24.0, right: 24.0),
      children: <Widget>[
      logo,
      SizedBox(height: 48.0),
      email,
      SizedBox(height: 8.0),
      password,
      SizedBox(height: 24.0),
      loginButton,
      forgotLabel
      ],
      ),
      ),
      );




      The scaffold return a error but i don´t know how can i fix this without rewrite all code.



      So the question is: How can i to show the snackbar when login fails and avoid this error? And why this error appears?



      UPDATED



       void initState() 
      super.initState();
      final snackBar = SnackBar(
      content: Text(
      'Usuario/Contraseña incorrecto',
      textAlign: TextAlign.center,
      ));
      Scaffold.of(context).showSnackBar(snackBar);
      seriesList = _createSampleData();
      animate = false;



      And how can i show snackbar when init page?







      dart flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:10

























      asked Nov 10 at 18:21









      El Hombre Sin Nombre

      155214




      155214






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          This is because you are using the context of the widget that creates the Scaffold (the parent context), not the context of the Scaffold itself. Thus the error.



          You can fix the error either by creating a method builder that will receive the correct context:



          Widget _buildLoginButton(BuildContext context) 
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );



          And refactor the page to use the builder method you've just created:



          Scaffold(
          appBar: AppBar(
          title: Text('My Page'),
          ),
          body: Builder(
          builder: (context) =>
          Column(
          children: [
          .....
          _buildLoginButton(context),
          .....
          ]
          ),
          ),
          ),
          );


          Or just extract the login button to its own Widget, without changing any other of your code, and it will receive the proper context.



          class LoginButton extends StatelessWidget 
          @override
          Widget build(BuildContext context)
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );







          share|improve this answer






















          • Can you put example extracting the button on is widget?
            – El Hombre Sin Nombre
            Nov 10 at 19:29










          • @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
            – shaddy
            Nov 10 at 20:01










          • I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
            – El Hombre Sin Nombre
            Nov 10 at 20:11










          • @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
            – shaddy
            Nov 10 at 20:51










          • I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
            – El Hombre Sin Nombre
            Nov 11 at 12:10










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242051%2fcan%25c2%25b4t-show-snackbar-in-flutter-login%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          This is because you are using the context of the widget that creates the Scaffold (the parent context), not the context of the Scaffold itself. Thus the error.



          You can fix the error either by creating a method builder that will receive the correct context:



          Widget _buildLoginButton(BuildContext context) 
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );



          And refactor the page to use the builder method you've just created:



          Scaffold(
          appBar: AppBar(
          title: Text('My Page'),
          ),
          body: Builder(
          builder: (context) =>
          Column(
          children: [
          .....
          _buildLoginButton(context),
          .....
          ]
          ),
          ),
          ),
          );


          Or just extract the login button to its own Widget, without changing any other of your code, and it will receive the proper context.



          class LoginButton extends StatelessWidget 
          @override
          Widget build(BuildContext context)
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );







          share|improve this answer






















          • Can you put example extracting the button on is widget?
            – El Hombre Sin Nombre
            Nov 10 at 19:29










          • @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
            – shaddy
            Nov 10 at 20:01










          • I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
            – El Hombre Sin Nombre
            Nov 10 at 20:11










          • @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
            – shaddy
            Nov 10 at 20:51










          • I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
            – El Hombre Sin Nombre
            Nov 11 at 12:10














          up vote
          2
          down vote



          accepted










          This is because you are using the context of the widget that creates the Scaffold (the parent context), not the context of the Scaffold itself. Thus the error.



          You can fix the error either by creating a method builder that will receive the correct context:



          Widget _buildLoginButton(BuildContext context) 
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );



          And refactor the page to use the builder method you've just created:



          Scaffold(
          appBar: AppBar(
          title: Text('My Page'),
          ),
          body: Builder(
          builder: (context) =>
          Column(
          children: [
          .....
          _buildLoginButton(context),
          .....
          ]
          ),
          ),
          ),
          );


          Or just extract the login button to its own Widget, without changing any other of your code, and it will receive the proper context.



          class LoginButton extends StatelessWidget 
          @override
          Widget build(BuildContext context)
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );







          share|improve this answer






















          • Can you put example extracting the button on is widget?
            – El Hombre Sin Nombre
            Nov 10 at 19:29










          • @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
            – shaddy
            Nov 10 at 20:01










          • I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
            – El Hombre Sin Nombre
            Nov 10 at 20:11










          • @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
            – shaddy
            Nov 10 at 20:51










          • I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
            – El Hombre Sin Nombre
            Nov 11 at 12:10












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          This is because you are using the context of the widget that creates the Scaffold (the parent context), not the context of the Scaffold itself. Thus the error.



          You can fix the error either by creating a method builder that will receive the correct context:



          Widget _buildLoginButton(BuildContext context) 
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );



          And refactor the page to use the builder method you've just created:



          Scaffold(
          appBar: AppBar(
          title: Text('My Page'),
          ),
          body: Builder(
          builder: (context) =>
          Column(
          children: [
          .....
          _buildLoginButton(context),
          .....
          ]
          ),
          ),
          ),
          );


          Or just extract the login button to its own Widget, without changing any other of your code, and it will receive the proper context.



          class LoginButton extends StatelessWidget 
          @override
          Widget build(BuildContext context)
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );







          share|improve this answer














          This is because you are using the context of the widget that creates the Scaffold (the parent context), not the context of the Scaffold itself. Thus the error.



          You can fix the error either by creating a method builder that will receive the correct context:



          Widget _buildLoginButton(BuildContext context) 
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );



          And refactor the page to use the builder method you've just created:



          Scaffold(
          appBar: AppBar(
          title: Text('My Page'),
          ),
          body: Builder(
          builder: (context) =>
          Column(
          children: [
          .....
          _buildLoginButton(context),
          .....
          ]
          ),
          ),
          ),
          );


          Or just extract the login button to its own Widget, without changing any other of your code, and it will receive the proper context.



          class LoginButton extends StatelessWidget 
          @override
          Widget build(BuildContext context)
          return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
          borderRadius: BorderRadius.circular(30.0),
          child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: ()
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
          ,
          color: Colors.blue[300],
          child: Text('Entrar', style: TextStyle(color: Colors.white)),
          ),
          ),
          );








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 20:01

























          answered Nov 10 at 18:52









          shaddy

          6,00831430




          6,00831430











          • Can you put example extracting the button on is widget?
            – El Hombre Sin Nombre
            Nov 10 at 19:29










          • @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
            – shaddy
            Nov 10 at 20:01










          • I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
            – El Hombre Sin Nombre
            Nov 10 at 20:11










          • @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
            – shaddy
            Nov 10 at 20:51










          • I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
            – El Hombre Sin Nombre
            Nov 11 at 12:10
















          • Can you put example extracting the button on is widget?
            – El Hombre Sin Nombre
            Nov 10 at 19:29










          • @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
            – shaddy
            Nov 10 at 20:01










          • I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
            – El Hombre Sin Nombre
            Nov 10 at 20:11










          • @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
            – shaddy
            Nov 10 at 20:51










          • I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
            – El Hombre Sin Nombre
            Nov 11 at 12:10















          Can you put example extracting the button on is widget?
          – El Hombre Sin Nombre
          Nov 10 at 19:29




          Can you put example extracting the button on is widget?
          – El Hombre Sin Nombre
          Nov 10 at 19:29












          @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
          – shaddy
          Nov 10 at 20:01




          @ElHombreSinNombre Basically create new stateless widget, I've updated my answer
          – shaddy
          Nov 10 at 20:01












          I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
          – El Hombre Sin Nombre
          Nov 10 at 20:11




          I updated my question. Your answer is correct, i test, but i need to show snackbar on init method.
          – El Hombre Sin Nombre
          Nov 10 at 20:11












          @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
          – shaddy
          Nov 10 at 20:51




          @ElHombreSinNombre During the init there is no widget tree yet, and the it requires you to have one in order to find the scaffold, you have to first assign a global key to your scaffold when it is rendered and invoke the scaffold with a delay in the init method referencing that global key: void initState() super.initState(); Future.delayed(Duration(seconds: 1)).then( (_) => _displaySnackbar ); void get _displaySnackbar _scaffoldKey.currentState.showSnackBar(SnackBar( duration: Duration(minutes: 1), content: Text('Your snackbar message') ));
          – shaddy
          Nov 10 at 20:51












          I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
          – El Hombre Sin Nombre
          Nov 11 at 12:10




          I try your code but something fails.gist.github.com/ElHombreSinNombre/… Can you code example? Return 'method showsnackbar was called on null'
          – El Hombre Sin Nombre
          Nov 11 at 12:10

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242051%2fcan%25c2%25b4t-show-snackbar-in-flutter-login%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党