NoSuchMethodError: The method 'validate' was called on null.

6,836 views
Skip to first unread message

Mahmoud Alminawi

unread,
Nov 26, 2019, 7:16:22 PM11/26/19
to Flutter Development (flutter-dev)
Hello all, I am getting this error can you please help me.  [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'validate' was called on null.
E/flutter (13281): Receiver: null
E/flutter (13281): Tried calling: validate()
E/flutter (13281): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)

here is the code for the log in page 


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 GlobalKey<FormState> _formkey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {

return Scaffold(


appBar: AppBar(
title: const Text('AppBar Ddemo'),
actions: <Widget>[


],
),
body:Form(

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

TextFormField(
autovalidate: (),
validator: (input){
if(input.length < 6){
return 'Please enter you r 6 or over passport';
}
//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
},
)
],
),

),
);
}


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

if(formState1.validate()){
formState1.save();



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

Gaurav Shetty

unread,
Nov 26, 2019, 7:40:39 PM11/26/19
to Flutter Development (flutter-dev)
You need add key to Form widget.
Form(key:_formkey)

Oséias Silva

unread,
Apr 2, 2020, 7:55:51 AM4/2/20
to Flutter Development (flutter-dev)
Hello, about this problem, I have the same problem and not resolved. Help me please!

Graham Dickinson

unread,
Apr 2, 2020, 8:30:30 AM4/2/20
to Flutter Development (flutter-dev)
Suggest you show us some code. The fix indicated in the previous post was correct for his code.

Souvik Dutta

unread,
Apr 2, 2020, 8:45:11 AM4/2/20
to Graham Dickinson, Flutter Development (flutter-dev)
You forgot to provide key inside the form in your login page. Just add this after you started the form widget.
key: _formkey, 
This should solve the problem.


--
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/df15369f-cf1e-4d79-ba54-4dba8969d17d%40googlegroups.com.

Oséias Silva

unread,
Apr 2, 2020, 8:58:13 AM4/2/20
to Flutter Development (flutter-dev)
Is running ok, thanks!

Souvik Dutta

unread,
Apr 2, 2020, 9:00:40 AM4/2/20
to Oséias Silva, Flutter Development (flutter-dev)
👍

On Thu, Apr 2, 2020, 6:28 PM Oséias Silva <10.o...@gmail.com> wrote:
Is running ok, thanks!


--
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.

Salsabeela H

unread,
Apr 3, 2020, 1:02:10 PM4/3/20
to Flutter Development (flutter-dev)

 I have the same problem and not resolved. Help me please! 
بتاريخ الخميس، 2 أبريل، 2020 4:00:40 م UTC+3، كتب Souvik Dutta:
👍

On Thu, Apr 2, 2020, 6:28 PM Oséias Silva <10.o...@gmail.com> wrote:
Is running ok, thanks!

--
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 flutt...@googlegroups.com.

Souvik Dutta

unread,
Apr 3, 2020, 9:12:05 PM4/3/20
to Salsabeela H, Flutter Development (flutter-dev)
Salsabeela provide your code.

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/564bf28d-cfe0-4ff5-b541-4894957fdfff%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages