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

1,101 views
Skip to first unread message

Mahmoud Alminawi

unread,
Nov 27, 2019, 10:21:00 AM11/27/19
to Flutter Development (flutter-dev)

I have this error I have no idea what it means. please help!

E/flutter (17371): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold.
E/flutter (17371): No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This usually happens when the context provided is from the same StatefulWidget as that whose build function actually creates the Scaffold widget being sought.
E/flutter (17371): There are several ways to avoid this problem. The simplest is to use a Builder to get a context that is "under" the Scaffold. For an example of this, please see the documentation for Scaffold.of():
E/flutter (17371): A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the Scaffold. In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets, and then in these inner widgets you would use Scaffold.of().
E/flutter (17371): A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
E/flutter (17371): The context used was:
E/flutter (17371):   LoginPage(state: _LoginPageState#93206)





here is my code: for the log in file 

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_app_dating/pages/home.dart';




class LoginPage extends StatefulWidget {

@override
_LoginPageState createState( ) => _LoginPageState();



}

class _LoginPageState extends State<LoginPage> {
String _email, _password;
final _formkey = GlobalKey<FormState>();

//final GlobalKey<FormState> _formkey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {

return Form(
key: _formkey,




child: Scaffold(

body:Form(
child: Column(
children: <Widget>[
TextFormField(
validator: (input){
if(input.isEmpty){
return 'Please put you remai';
}
return null;
},
onSaved: (input) => _email = input ,
decoration: InputDecoration(
labelText: 'Email'
) ,
),

TextFormField(
validator: (input){
if(input.length < 6){
return 'Please enter you r 6 or over passport';
}
return null;

//return(input);
},
onSaved: (input) => _password = input ,
decoration: InputDecoration(
labelText: 'Enter you rpass'
) ,
obscureText: true,
),
RaisedButton(


onPressed:(){
SizedBox(
child: Text('dfedc '),
height: 22,
width: 52,


);

},
child: Text('sighn ic '),
),

/*
IconButton(
icon: Icon(Icons.info),
onPressed:(){ signIn();
// Do something
},
),*/



Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () {

signIn();
},
child: Text('Submit'),
),
),




],
),
),
),


);
}


Future<void> signIn() async{

final formState1 = _formkey.currentState;

if (_formkey.currentState.validate()) {
formState1.save();
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}




try{
FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
Navigator.push(context, MaterialPageRoute(builder: (context) => Home() ));

}catch(e){
print(e.message);
}

}



}








HERE IS THE CODE FOR THE HOME PAGE IN A DIFFRENT DART FILE 

import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
);
}
}








HERE IS THE CODE FOR THE MAIN FILe

import 'package:flutter/material.dart';
import 'package:flutter_app_dating/SetUp/Login.dart';

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demhho',
theme: ThemeData(
primarySwatch: Colors.blue,
),

home: LoginPage(),
);
}
}

Rex Bloom

unread,
Nov 27, 2019, 10:59:32 AM11/27/19
to Mahmoud Alminawi, Flutter Development (flutter-dev)
Where is the context from the call to Scaffold.of(context) declared?  Typically you would pass the build context into your signIn function to have it available for this type of scenario.

Rex

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/3c230bac-e0b5-4692-a537-6d0aa95b4a76%40googlegroups.com.

Mahmoud Alminawi

unread,
Nov 27, 2019, 11:07:27 AM11/27/19
to Flutter Development (flutter-dev)
I have solved this issue, i ran into another issue i guess problem with nulls. 


Screenshot (137).png

i get this error every time i hit submit for the login. 

Rex Bloom

unread,
Nov 27, 2019, 11:19:04 AM11/27/19
to Mahmoud Alminawi, Flutter Development (flutter-dev)
Paste your relevant code to this thread and I can take a look.


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Mahmoud Alminawi

unread,
Nov 27, 2019, 11:21:29 AM11/27/19
to Flutter Development (flutter-dev)
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_app_dating/pages/home.dart';




class LoginPage extends StatefulWidget {

@override
_LoginPageState createState( ) => _LoginPageState();



}

class _LoginPageState extends State<LoginPage> {
String _email, _password;
final _formkey = GlobalKey<FormState>();
  final _Scaffold = GlobalKey<FormState>();


//final GlobalKey<FormState> _formkey = GlobalKey<FormState>();

@override
Widget build( BuildContext context ) {
return Form(

key: _formkey,



child: Scaffold(
key: _Scaffold,

body: Form(
child: Column(
children: <Widget>[
TextFormField(
validator: ( input ) {
if (input.isEmpty) {
return 'Please put you remai';
}
return null;
},
onSaved: ( input ) => _email = input,
decoration: InputDecoration(
labelText: 'Email'
),
),

TextFormField(
validator: ( input ) {
                  if (input.length < 6) {

return 'Please enter you r 6 or over passport';
}
return null;

//return(input);
},
onSaved: ( input ) => _password = input,
decoration: InputDecoration(
labelText: 'Enter you rpass'
),
obscureText: true,
),


              /*
RaisedButton(


onPressed: ( ) {
                  SizedBox(
child: Text('dfedc '),
height: 22,
width: 52,


);
},
child: Text('sighn ic '),
),

               */

/*
IconButton(
icon: Icon(Icons.info),
onPressed:(){ signIn();
// Do something
},
),*/


Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed:
                    signIn,


child: Text('Submit'),
),
),


],
),
),
),


);
}


Future<void> signIn() async {
    final FormState = _formkey.currentState;

if (_formkey.currentState.validate()) {
FormState.save();
/*
      Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));

       */
}

Text('Submit');




try{
FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
Navigator.push(context, MaterialPageRoute(builder: (context) => Home()));

}catch(e){
print(e.message);
}

}



}








and here is the code for the home dart file 



import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
);
}
}








and here is the code for the main file



import 'package:flutter/material.dart';
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Neriya Rosner

unread,
Nov 27, 2019, 11:26:05 AM11/27/19
to Flutter Development (flutter-dev), Mahmoud Alminawi
You need to place the scaffold in the parent so you could reference by context, or you can use a local key to reference the right widget.

From: flutt...@googlegroups.com <flutt...@googlegroups.com> on behalf of Mahmoud Alminawi <malm...@drew.edu>
Sent: Wednesday, November 27, 2019 6:21:29 PM
To: Flutter Development (flutter-dev) <flutt...@googlegroups.com>
Subject: Re: Scaffold.of() called with a context that does not contain a Scaffold.
 
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/1445724e-d5a0-4676-b8a3-32a8a5898ad8%40googlegroups.com.

Rex Bloom

unread,
Nov 27, 2019, 11:29:06 AM11/27/19
to Mahmoud Alminawi, Flutter Development (flutter-dev)
Try my solution for Scaffold and remove the key on your scaffold.  I don't think you want to create two GlobalKey object like you are doing.

I think this may solve your new issue.

Rex

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/1445724e-d5a0-4676-b8a3-32a8a5898ad8%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages