简体   繁体   中英

Null check operator used on a null value in flutter using Textformfield

I am new at flutter and in this simple code i am getting an error that 'NUll check operator used on a null value' and i tried all the solutions that were present here(I run all the commands in my terminal like flutter upgrade and so on) but still i am getting the same error. Please provide me a solution for this, i am stucked here from some days.

 import 'package:flutter/material.dart';

   Future<void> main() async{
    WidgetsFlutterBinding.ensureInitialized();

   runApp(MaterialApp(
   home: Home(),
   ));
  }

  class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
 _HomeState createState() => _HomeState();
  }

  class _HomeState extends State<Home> {

 final  _key =GlobalKey<FormState>();
  final textformfield=TextEditingController();

 @override
  Widget build(BuildContext context) {
  return Scaffold(
   body: Center(
     child: Container(
      child: Column(
        children: [
          SizedBox(height: 300,),
          TextFormField(
            controller: textformfield,
            key: _key,
            validator: (value){
              if(value!.isEmpty){
                return "Please enter your email";
              }
              else return null;
            },
          ),
          FlatButton(
              onPressed: (){
                 if(_key.currentState!.validate()){
                            print("validate");
                 }
                 else print('NUll');
              },
              child: Text('Validate')
                ),
              ],
            ),
         ),
        ),
      );
     }
    }

Thanks in advance.!

First, you need to have a Form Widget and put TextFormFields inside it. https://docs.flutter.dev/cookbook/forms/validation

return Form(
  key: _key,
  child: Column(
    children: <Widget>[
      // Add TextFormFields and ElevatedButton here.
    ],
  ),
);

Try below code hope its help to you. add your widgets inside Form

refer form Validation here

  Form(
        key: _key,
        child: Column(
          children: [
            SizedBox(
              height: 300,
            ),
            TextFormField(
              controller: textformfield,
              validator: (value) {
                if (value!.isEmpty) {
                  return "Please enter your email";
                } else
                  return null;
              },
            ),
            TextButton(
                onPressed: () {
                  if (_key.currentState!.validate()) {
                    print("validate");
                  } else
                    print('NUll');
                },
                child: Text('Validate')),
          ],
        ),
      ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM