错误:当我在颤振验证器之后运行应用程序时,颤振中出现意外的空值
Posted
技术标签:
【中文标题】错误:当我在颤振验证器之后运行应用程序时,颤振中出现意外的空值【英文标题】:Error : Unexpected null value in flutter when I run app after flutter validator 【发布时间】:2021-09-15 13:53:09 【问题描述】:当我使用 if 语句来验证错误时,会出现谈论这个 if 语句: (完整代码在最后)
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;
);
当我点击正在使用它的按钮时,会出现以下所有错误
onTap: () => movetoHome(context),
由 Inkwell 类包装。
如果我使用 f5 运行,我会在 feedback.dart 中达到断点 第 107 行
return Future.value();
如果我使用终端运行它会给我这个错误:
错误:意外的空值。 在 Object.throw_ [as throw] (http://localhost:53848/dart_sdk.js:5348:11) 在 Object.nullCheck (http://localhost:53848/dart_sdk.js:5681:30) 在 login_page._LoginPageState.new.movetoHome (http://localhost:53848/packages/learning/pages/login_page.dart.lib.js:644:28) 在 movetoHome.next () 在 runBody (http://localhost:53848/dart_sdk.js:39250:34) 在 Object._async [as async] (http://localhost:53848/dart_sdk.js:39281:7) 在 login_page._LoginPageState.new.movetoHome (http://localhost:53848/packages/learning/pages/login_page.dart.lib.js:643:20) 在 http://localhost:53848/packages/learning/pages/login_page.dart.lib.js:677:569 在 ink_well._InkResponseState.new.[_handleTap] (http://localhost:53848/packages/flutter/src/material/icon_button.dart.lib.js:50393:31) 在 tap.TapGestureRecognizer.new.invokeCallback (http://localhost:53848/packages/flutter/src/gestures/recognizer.dart.lib.js:194:18) 在 tap.TapGestureRecognizer.new.handleTapUp (http://localhost:53848/packages/flutter/src/gestures/tap.dart.lib.js:408:40) 在 tap.TapGestureRecognizer.new.[_checkUp] (http://localhost:53848/packages/flutter/src/gestures/tap.dart.lib.js:214:12) 在 tap.TapGestureRecognizer.new.handlePrimaryPointer (http://localhost:53848/packages/flutter/src/gestures/tap.dart.lib.js:160:23) 在 tap.TapGestureRecognizer.new.handleEvent (http://localhost:53848/packages/flutter/src/gestures/recognizer.dart.lib.js:449:16) 在 pointer_router.PointerRouter.new.[_dispatch] (http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:93:9) 在 http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:128:26 在 LinkedMap.new.forEach (http://localhost:53848/dart_sdk.js:26346:11) 在pointer_router.PointerRouter.new.[_dispatchEventToRoutes] (http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:125:29) 在 pointer_router.PointerRouter.new.route (http://localhost:53848/packages/flutter/src/gestures/pointer_router.dart.lib.js:117:37) 在绑定$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:389:26) 在 binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:372:24) 在绑定$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:53848/packages/flutter/src/rendering/layer.dart.lib.js:5376:13) 在绑定$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:343:14) 在绑定$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:316:43) 在绑定$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:305:14) 在绑定$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:53848/packages/flutter/src/gestures/binding.dart.lib.js:295:65) 在 Object.invoke1 (http://localhost:53848/dart_sdk.js:186355:7) 在 _engine.EnginePlatformDispatcher..invokeOnPointerDataPacket (http://localhost:53848/dart_sdk.js:166281:15) 在 _engine.PointerBinding..[_onPointerData] (http://localhost:53848/dart_sdk.js:166941:49) 在 http://localhost:53848/dart_sdk.js:167399:28 在 http://localhost:53848/dart_sdk.js:167352:16 在 http://localhost:53848/dart_sdk.js:167045:11
我的登录页面完整代码:
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,
),
),
),
],
),
),
));
【问题讨论】:
你能在movetoHome
方法的每一行后面写打印语句来准确地查看代码中断的地方吗?
@Nicks101 我使用了 print 语句,但没有输出 我也在 if 语句之前使用了 print 语句,但再次没有输出,但是当我删除 if 语句时,除了验证之外,一切都开始工作而没有错误。
您能否显示使用_formKey
的代码(赋值、声明等)?
@Nicks101 child: Column( key: _formKey, children: [ TextFormField( validator: (value) if (value!.isEmpty) return "用户名不能为空"; return null; ,
@Nicks101 顺便说一句,我在问题描述中分享我的整个代码,因为我不能在这里分享整个代码。
【参考方案1】:
_formKey
分配的 Widget 错误。
它应该分配给Form
的key属性,而不是任何其他Widget。
Form(
key: _formKey, //// Assign here
)
参考flutter官方文档here
【讨论】:
非常感谢哥们,过去 4 小时里我犯了一个小错误。以上是关于错误:当我在颤振验证器之后运行应用程序时,颤振中出现意外的空值的主要内容,如果未能解决你的问题,请参考以下文章