我正在快速创建一个SignUp表单,并且尝试在TextFormField上应用验证器,并在用户忘记填写他收到验证警报消息的字段时调用。但是我不知道为什么它不起作用。问题是我无法理解这里的事实。如果有人知道,请帮助解决我的问题。我收到此错误:
I/flutter (29245): The getter 'current' was called on null.
I/flutter (29245): Receiver: null
I/flutter (29245): Tried calling: current
I/flutter (29245): The relevant error-causing widget was:
I/flutter (29245): AnimatedContainer
lib\signuppage.dart:147
I/flutter (29245): When the exception was thrown, this was the stack:
// *这是我的代码
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:login_page_flutter/Models/myconstant.dart';
import 'package:login_page_flutter/Models/user.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:login_page_flutter/loginpage.dart';
class SignUpPage extends StatefulWidget {
@override
_SignUpPageState createState() => _SignUpPageState();
}
class _SignUpPageState extends State<SignUpPage> {
var _formKey = GlobalKey<FormState>();
TextEditingController txtname = TextEditingController();
TextEditingController txtusername = TextEditingController();
TextEditingController txtpassword = TextEditingController();
//TextEditingController txttype = TextEditingController();
bool isUploading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Neumorphic(
child: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.all(20.0),
child: ListView(
children: <Widget>[
SizedBox(
height: 10,
),
Center(
child: Container(
child: Center(
child: Text(
'SignUp',
style: TextStyle(
color: Colors.pink[800],
decoration:
TextDecoration.combine(List<TextDecoration>()),
),
),
),
width: 100,
height: 50,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.all(Radius.circular(78)),
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(4.0, 4.0),
blurRadius: 10.0,
spreadRadius: 1.0,
),
BoxShadow(
color: Colors.white,
offset: Offset(-4.0, -4.0),
blurRadius: 10.0,
spreadRadius: 1.0,
),
]),
),
),
SizedBox(
height: 20,
),
Card(
child: Neumorphic(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
SizedBox(
height: 10,
),
Neumorphic(
style: NeumorphicStyle(
boxShape: NeumorphicBoxShape.roundRect(
BorderRadius.circular(10)),
),
child: Container(
decoration: BoxDecoration(boxShadow: [
BoxShadow(
color: Colors.white,
offset: Offset(4.0, 4.0),
blurRadius: 10.0,
spreadRadius: 1.0,
),
BoxShadow(
color: Colors.grey[300],
offset: Offset(-4.0, -4.0),
blurRadius: 10.0,
spreadRadius: 1.0,
),
]),
child: TextFormField(
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter Name';
}
return null;
},
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.pink),
),
controller: txtname,
),
),
),
TextFormField(
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter userName';
}
return null;
},
controller: txtusername,
),
TextFormField(
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter Password';
}
return null;
},
controller: txtpassword,
),
SizedBox(
height: 10,
),
],
),
style: NeumorphicStyle(
boxShape: NeumorphicBoxShape.roundRect(
BorderRadius.circular(10)),
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
),
),
SizedBox(height: 30),
Center(
child: AnimatedContainer(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
blurRadius: 15,
offset: -Offset(5, 5),
color: Colors.white),
BoxShadow(
blurRadius: 15,
offset: Offset(4.5, 4.5),
color: Colors.white38),
],
),
duration: const Duration(milliseconds: 50),
child: getWidgetByStatus(),
),
),
],
),
),
),
),
);
}
Future<void> saveData(dynamic jsonOfuser) async {
setState(() {
isUploading = true;
});
await Future.delayed(Duration(seconds: MyConstant.VAL_SECONDS), () {});
return post(
MyConstant.URL_USER_INSERT,
body: jsonOfuser,
).then((onRecievedResponse) {
print(onRecievedResponse.body);
setState(() {
isUploading = false;
});
return null;
});
}
Widget getWidgetByStatus() {
if (isUploading) {
return CircularProgressIndicator(
backgroundColor: Colors.pink,
);
} else {
return NeumorphicButton(
onPressed: () {
User user = User(
id: 11,
name: txtname.text,
username: txtusername.text,
password: txtpassword.text,
type: 11,
);
saveData(
User.toJson(user),
);
if (_formKey.currentState.validate()) {
setState(
() {
SnackBar(
backgroundColor: Colors.white60,
content: Row(
children: <Widget>[
Icon(Icons.thumb_up, color: Colors.pink),
SizedBox(width: 10),
Expanded(
child: Text(
'Data Inserted Successfully',
style: TextStyle(color: Colors.pink),
),
)
],
),
);
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LoginPage();
}));
},
);
}
},
child: Text(
'Submit',
style: TextStyle(color: Colors.pink[800]),
),
);
}
}
}
这是控制台
I/flutter (29245): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (29245): The following NoSuchMethodError was thrown building Container(bg:
I/flutter (29245): BoxDecoration(
I/flutter (29245): boxShadow: [BoxShadow(Color(0xffffffff), Offset(-5.0, -5.0), 15.0,
I/flutter (29245): 0.0), BoxShadow(Color(0x62ffffff), Offset(4.5, 4.5), 15.0, 0.0)]
I/flutter (29245): )
I/flutter (29245): ):
I/flutter (29245): The getter 'current' was called on null.
I/flutter (29245): Receiver: null
I/flutter (29245): Tried calling: current
I/flutter (29245): The relevant error-causing widget was:
[38;5;248mI/flutter (29245): AnimatedContainer[39;49m
I/flutter (29245): When the exception was thrown, this was the stack:
[38;5;244mI/flutter (29245): #0 Object.noSuchMethod (dart:core-