【发布时间】:2021-08-24 21:35:43
【问题描述】:
我有一个问题,我正在使用颤振创建一个应用程序,当我想声明一个变量并在之后初始化它时,我有一个错误“错误:字段'_prenom'应该被初始化,因为它的类型'String'没有允许为空。”我测试了很多东西,但一切正常
import 'package:flutter/material.dart';
class LoginController extends StatefulWidget {
LoginControllerState createState() => new LoginControllerState();
}
class LoginControllerState extends State<LoginController> {
bool _log = true;
String _mail;
String _password;
String _prenom;
String _nom;
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(title: new Text("Authentification"),),
body: new SingleChildScrollView(
child: new Column(
children: <Widget>[
new Container(
margin: EdgeInsets.all(20.0),
width: MediaQuery.of(context).size.width - 40,
height: MediaQuery.of(context).size.height / 2,
child: new Card(
elevation: 8.5,
child: new Column(
children: cardElements(),
),
),
)
],
),
),
);
}
List<Widget> cardElements(){
List<Widget> widgets = [];
widgets.add(
new TextField(
decoration: new InputDecoration(hintText: "Entrez votre adresse mail"),
onChanged: (string) {
setState(() {
_mail = string;
});
},
)
);
widgets.add(
new TextField(
decoration: new InputDecoration(hintText: "Entrez votre mot de passe"),
onChanged: (string) {
setState(() {
_password = string;
});
},
)
);
if(_log == false ) {
widgets.add(
new TextField(
decoration: new InputDecoration(hintText: "Entrez votre prenom"),
onChanged: (string) {
setState(() {
_prenom = string;
});
},
)
);
widgets.add(
new TextField(
decoration: new InputDecoration(hintText: "Entrez votre nom"),
onChanged: (string) {
setState(() {
_nom = string;
});
},
)
);
}
widgets.add(
new TextButton(
onPressed: () {
setState(() {
_log = !_log;
});
},
child: new Text(
(_log == true)
? "Pour créer un compte, appuyez ici"
: "Vous avez déjà un compte ? Appuyez ici"
)
)
);
return widgets;
}
}
谢谢你的回答...
【问题讨论】: