''''我是Flutter的初学者,这是问题所在-我试图用验证和'Submit'按钮构建表单,该表单必须控制文本字段,但是发生了异常。 NoSuchMethodError(NoSuchMethodError:方法'validate'在null上调用。 接收者:null 尝试调用:validate())。这是代码:“''
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';
class SharedKonusu extends StatefulWidget {
@override
State<StatefulWidget> createState() => ShareState();
}
class ShareState extends State<SharedKonusu> {
final isimCtrl = TextEditingController();
final soyisimCtrl = TextEditingController();
final globalCtrl = GlobalKey<FormState>();
String isimStr = "";
String soyisimStr = "";
bool kayitDurum = false;
int kayitNo = 0;
@override
void dispose() {
isimCtrl.dispose();
soyisimCtrl.dispose();
super.dispose();
}
void kayitYap(String gIsim, String gSisim) async {
final registeR = await SharedPreferences.getInstance();
if (globalCtrl.currentState.validate()) {
registeR.setBool("durum", true);
registeR.setInt("kayitNo", 1);
registeR.setString("isim", gIsim);
registeR.setString("soyisim", gSisim);
Fluttertoast.showToast(
msg: "Registering...",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM);
}
}
void getData() async {
final registeR = await SharedPreferences.getInstance();
bool kDurum = registeR.getBool("durum");
int kNo = registeR.getInt("kayitNo");
String kIsim = registeR.getString("isim");
String kSisim = registeR.getString("soyisim");
setState(() {
kayitDurum = kDurum;
kayitNo = kNo;
isimStr = kIsim;
soyisimStr = kSisim;
});
Fluttertoast.showToast(
msg: "Getting data...",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
}
void deleteData() async {
final registeR = await SharedPreferences.getInstance();
registeR.remove("isim");
registeR.remove("soysisim");
registeR.remove("ksyitNo");
registeR.remove("durum");
Fluttertoast.showToast(
msg: "Getting data...",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("My Title"),
),
body: Form(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
validator: (deger) {
if (deger.isEmpty) {
return "Incorrect Name";
}
},
controller: isimCtrl,
decoration: InputDecoration(hintText: "Your Name"),
),
TextFormField(
validator: (sname) {
if (sname.isEmpty) {
return "Incorrect Surname";
}
},
controller: soyisimCtrl,
decoration: InputDecoration(
hintText: "Your surname",
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
onPressed: () =>
kayitYap(isimCtrl.text, soyisimCtrl.text),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(50.0),
),
color: Colors.blue,
child: Text(
"Register",
style: TextStyle(color: Colors.white),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
onPressed: () => getData(),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(50.0),
),
color: Colors.green,
child: Text(
"Get data",
style: TextStyle(color: Colors.white),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
onPressed: () => deleteData(),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(50.0),
),
color: Colors.red,
child: Text(
"Delete data",
style: TextStyle(color: Colors.white),
),
),
),
),
],
),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Text("Name: $isimStr"),
Text("Surname:$soyisimStr"),
Text("Registered:$kayitDurum"),
Text("Register No: $kayitNo"),
],
),
),
),
],
),
),
);
}
}
答案 0 :(得分:0)
这是因为您没有将formKey globalCtrl分配给Form小部件的键参数。
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';
class SharedKonusu extends StatefulWidget {
@override
State<StatefulWidget> createState() => ShareState();
}
class ShareState extends State<SharedKonusu> {
final isimCtrl = TextEditingController();
final soyisimCtrl = TextEditingController();
final globalCtrl = GlobalKey<FormState>();
String isimStr = "";
String soyisimStr = "";
bool kayitDurum = false;
int kayitNo = 0;
@override
void dispose() {
isimCtrl.dispose();
soyisimCtrl.dispose();
super.dispose();
}
void kayitYap(String gIsim, String gSisim) async {
final registeR = await SharedPreferences.getInstance();
if (globalCtrl.currentState.validate()) {
registeR.setBool("durum", true);
registeR.setInt("kayitNo", 1);
registeR.setString("isim", gIsim);
registeR.setString("soyisim", gSisim);
Fluttertoast.showToast(
msg: "Registering...",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM);
}
}
void getData() async {
final registeR = await SharedPreferences.getInstance();
bool kDurum = registeR.getBool("durum");
int kNo = registeR.getInt("kayitNo");
String kIsim = registeR.getString("isim");
String kSisim = registeR.getString("soyisim");
setState(() {
kayitDurum = kDurum;
kayitNo = kNo;
isimStr = kIsim;
soyisimStr = kSisim;
});
Fluttertoast.showToast(
msg: "Getting data...",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
}
void deleteData() async {
final registeR = await SharedPreferences.getInstance();
registeR.remove("isim");
registeR.remove("soysisim");
registeR.remove("ksyitNo");
registeR.remove("durum");
Fluttertoast.showToast(
msg: "Getting data...",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("My Title"),
),
body: Form(
key: globalCtrl,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
validator: (deger) {
if (deger.isEmpty) {
return "Incorrect Name";
}
},
controller: isimCtrl,
decoration: InputDecoration(hintText: "Your Name"),
),
TextFormField(
validator: (sname) {
if (sname.isEmpty) {
return "Incorrect Surname";
}
},
controller: soyisimCtrl,
decoration: InputDecoration(
hintText: "Your surname",
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
onPressed: () =>
kayitYap(isimCtrl.text, soyisimCtrl.text),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(50.0),
),
color: Colors.blue,
child: Text(
"Register",
style: TextStyle(color: Colors.white),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
onPressed: () => getData(),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(50.0),
),
color: Colors.green,
child: Text(
"Get data",
style: TextStyle(color: Colors.white),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
onPressed: () => deleteData(),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(50.0),
),
color: Colors.red,
child: Text(
"Delete data",
style: TextStyle(color: Colors.white),
),
),
),
),
],
),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Text("Name: $isimStr"),
Text("Surname:$soyisimStr"),
Text("Registered:$kayitDurum"),
Text("Register No: $kayitNo"),
],
),
),
),
],
),
),
);
}
}