Saleh Muahammad
Saleh Muahammad

Reputation: 75

Error : Unexpected null value in flutter when I run app after flutter validator

when I use if statement to validate error appears talking about this if statement: (entire code at end)

 movetoHome(BuildContext context) async {
    if (_formKey.currentState!.validate()) {
      setState(() {
        changebutton = true;
      });
      await Future.delayed(Duration(seconds: 1));
      await Navigator.pushNamed(context, Myroutes.homeRoute);
      setState(() {
        changebutton = false;
      });
    }
}

All of the errors below appears when i click on button which is using this

onTap: () => movetoHome(context),

Wrapped by Inkwell Class.

  1. If I run using f5 it takes me to breaking point in feedback.dart at line 107

    return Future.value();

  2. if I run using terminal it gives me this error:

    Error: Unexpected null value. at Object.throw_ [as throw] (http://localhost:53848/dart_sdk.js:5348:11) at Object.nullCheck (http://localhost:53848/dart_sdk.js:5681:30) at login_page._LoginPageState.new.movetoHome (http://localhost:53848/packages/learning/pages/login_page.dart.lib.js:644:28) at movetoHome.next () at runBody (http://localhost:53848/dart_sdk.js:39250:34) at Object._async [as async] (http://localhost:53848/dart_sdk.js:39281:7) at login_page._LoginPageState.new.movetoHome (http://localhost:53848/packages/learning/pages/login_page.dart.lib.js:643:20) at http://localhost:53848/packages/learning/pages/login_page.dart.lib.js:677:569 at ink_well._InkResponseState.new.[_handleTap] (http://localhost:53848/packages/flutter/src/material/icon_button.dart.lib.js:50393:31) at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:53848/packages/flutter/src/gestures/recognizer.dart.lib.js:194:18) at tap.TapGestureRecognizer.new.handleTapUp (http://localhost:53848/packages/flutter/src/gestures/tap.dart.lib.js:408:40) at tap.TapGestureRecognizer.new.[_checkUp] (http://localhost:53848/packages/flutter/src/gestures/tap.dart.lib.js:214:12) at tap.TapGestureRecognizer.new.handlePrimaryPointer (http://localhost:53848/packages/flutter/src/gestures/tap.dart.lib.js:160:23) at tap.TapGestureRecognizer.new.handleEvent (http://localhost:53848/packages/flutter/src/gestures/recognizer.dart.lib.js:449:16) at pointer_router.PointerRouter.new.[_dispatch] (http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:93:9) at http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:128:26 at LinkedMap.new.forEach (http://localhost:53848/dart_sdk.js:26346:11) at pointer_router.PointerRouter.new.[_dispatchEventToRoutes] (http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:125:29) at pointer_router.PointerRouter.new.route (http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:117:37) at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:389:26) at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:372:24) at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:53848/packages/flutter/src/rendering/layer.dart.lib.js:5376:13) at binding$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:343:14) at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:316:43) at binding$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:305:14) at binding$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:295:65) at Object.invoke1 (http://localhost:53848/dart_sdk.js:186355:7) at _engine.EnginePlatformDispatcher..invokeOnPointerDataPacket (http://localhost:53848/dart_sdk.js:166281:15) at _engine.PointerBinding..[_onPointerData] (http://localhost:53848/dart_sdk.js:166941:49) at http://localhost:53848/dart_sdk.js:167399:28 at http://localhost:53848/dart_sdk.js:167352:16 at http://localhost:53848/dart_sdk.js:167045:11

My login page entire code :

import 'package:flutter/material.dart';
import 'package:learning/pages/utis/routes.dart';

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

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

class _LoginPageState extends State<LoginPage> {
  String name = "";
  bool changebutton = false;
  final _formKey = GlobalKey<FormState>();



 movetoHome(BuildContext context) {
    if (_formKey.currentState!.validate()) {
      print("before if");

  setState(() {
    changebutton = true;
    print("Change button 1");
  });
  Future.delayed(Duration(seconds: 1));
  print("while await");
  Navigator.pushNamed(context, Myroutes.homeRoute);
  print("after Naviagtor");
  setState(() {
    changebutton = false;
  });
}


}



Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: Form(
          child: SingleChildScrollView(
            child: Column(
              children: [
                Image.asset(
                  "assets/images/login_image.png",
                  fit: BoxFit.cover,
                ),
                SizedBox(height: 10),
                Text(
                  "Welcome $name",
                  style: TextStyle(
                    fontSize: 25,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                ),
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 40, vertical: 30),
                  child: Column(
                    key: _formKey,
                    children: [
                      TextFormField(
                        validator: (value) {
                          if (value!.isEmpty) {
                            return "Username cannot be empty";
                          }

               

   return null;
                    },
                        decoration: InputDecoration(
                          labelText: "Username",
                          hintText: "Enter Username",
                        ),
                        onChanged: (value) {
                          name = value;
                          setState(() {});
                        },
                      ),
                      TextFormField(
                        validator: (value) {
                          if (value!.isEmpty) {
                            return "Password cant be Empty";
                          } else if (value.length < 6) {
                            return "Password should be greater the 6 ";
                          }
                          return null;
                        },
                        obscureText: true,
                        decoration: InputDecoration(
                          labelText: "Password",
                          hintText: "Enter password",
                        ),
                      )
                    ],
                  ),
                ),
                Material(
                  borderRadius: BorderRadius.circular(changebutton ? 80 : 10),
                  color: Colors.purple,
                  child: InkWell(
                    onTap: () => movetoHome(context),
                    child: AnimatedContainer(
                      duration: Duration(
                        seconds: 1,
                      ),
                      alignment: Alignment.center,
                      child: changebutton
                          ? Icon(
                              Icons.check,
                              color: Colors.white,
                            )
                          : Text(
                              "Login",
                              style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontSize: 16,
                              ),
                            ),
                      height: 40,
                      width: changebutton ? 40 : 100,
                    ),
                  ),
                ),
                
              ],
            ),
          ),
        ));
  }
}

Upvotes: 1

Views: 4603

Answers (1)

nicks101
nicks101

Reputation: 1191

The Widget where _formKey is assigned is wrong.

It should be assigned to key property of Form, not any other Widget.

Form(
  key: _formKey, //// Assign here
)

Refer flutter official docs here

Upvotes: 5

Related Questions