3

I am building a login screen that passes data to a home screen and when I click on the login button nothing happens. No error but also no sending to a new screen.

here is the button code:

    Widget submitButton()
    {
 return Container(
        margin: EdgeInsets.all(10.0),
        child:RaisedButton(
        child: Text('LOGIN'),
        onPressed: (){
          if(formKey.currentState.validate()){

            formKey.currentState.save();
             print('Time to save $emailAddress and $passWord to my API');
             Navigator.push(
               context,
                      new MaterialPageRoute(
                     builder: (BuildContext context) =>
                     new Home()));

                  }
               },

        )
 );

here is my routes.dart file

 import 'package:flutter/material.dart';
 import '../screens/home.dart';
 import 'package:lbconnectv3/main.dart';

 final routes = {
  '/home':         (BuildContext context) => new Home(),
   '/' :          (BuildContext context) => new LBConnectApp(),
   };

EDIT

FULL CODE for main.dart, it basically has a form with 2 text fields for a login. No errors in console and it enters the if statement.

import 'package:flutter/material.dart';
import 'utils/validation.dart';
import 'screens/home.dart';
import 'utils/routes.dart';

 void main() => runApp(new LBConnectApp());

  class LBConnectApp extends StatefulWidget {

  createState()
  {
     return new LBConnectAppState();
   }

   } 

   class LBConnectAppState extends State<LBConnectApp> with Validation
   {

      final formKey = GlobalKey<FormState>();
      String emailAddress = '';
      String passWord = '';
      Widget build(context) {
      return MaterialApp(

       title: "LB Connect",
        home: Scaffold(
        backgroundColor: const Color(0xFF0099a9),
        body:
        Container(
        margin: EdgeInsets.all(25.0),
        child: Form(
        key: formKey,
        child: Column(
         children: [
          Container(
            margin: EdgeInsets.only(top: 100.0),
            child:Image.asset('images/lb_connect_trans.png')
           ),
           emailField(),
           passwordField(),   
          submitButton(),
        ],
         ),      
        ),
       )
       ),  
       );  
       }

       Widget emailField()
       {
         return TextFormField(
         keyboardType: TextInputType.emailAddress,
          decoration : InputDecoration(
           labelText: 'Email Address',
         ),

         validator: validateEmail,
         onSaved: (String value){

           emailAddress = value;

         },

       );
       }

       Widget passwordField()
       {
           return TextFormField(
         obscureText: true,
         decoration : InputDecoration(
           labelText: 'Password',

         ),

         validator: validatePassword,
          onSaved: (String value){

            passWord = value;
         },

         );
       }

       Widget submitButton()
      {
          return Container(
        margin: EdgeInsets.all(10.0),
        child:RaisedButton(
        child: Text('LOGIN'),
        onPressed: (){
          if(formKey.currentState.validate()){

            formKey.currentState.save();
             print('Time to save $emailAddress and $passWord to my API');
             Navigator.push(
               context,
                      new MaterialPageRoute(
                     builder: (BuildContext context) =>
                     new Home()));

                  }
               },

            )
       );
     }

    }
7
  • Everything looks good to me. Is it entering into the if in your onPressed. Are you getting any error in console Aug 23, 2018 at 5:11
  • post full code, to see how do you use the widget
    – Tree
    Aug 23, 2018 at 11:11
  • and try to use pushNamed('/home');
    – Tree
    Aug 23, 2018 at 11:11
  • I'll edit and post full code. No errors in the console and it enters the if statement.
    – Sam Cromer
    Aug 23, 2018 at 13:23
  • full code posted for main.dart
    – Sam Cromer
    Aug 23, 2018 at 13:30

2 Answers 2

4

After messing around with my app I figured out the issue :)

I had to wrap my class with a Material App and then add my routes property to it. Here is the correct code:

 void main() => runApp(MaterialApp(
 routes: routes,
 home: LBConnectApp()
 )
);
0

Change this line

void main() => runApp(new LBConnectApp());

with this and it will work as per your expectation

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  return new MaterialApp(
  title: 'Flutter Demo',
  theme: new ThemeData(
    primarySwatch: Colors.blue,
  ),
  debugShowCheckedModeBanner: false,
  home: new LBConnectApp(),
);
}
}
3
  • that did not work. same error. Thats just the class name
    – Sam Cromer
    Aug 24, 2018 at 14:20
  • This is the error im trying to fix. it has to do with the context im using, im completely new to flutter so any suggestions would be great. ""
    – Sam Cromer
    Aug 24, 2018 at 16:04
  • The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
    – Sam Cromer
    Aug 24, 2018 at 16:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.