gpt4 book ai didi

flutter - 错误 : Error: Field '_prenom' should be initialized because its type 'String' doesn't allow null

转载 作者:行者123 更新时间:2023-12-05 08:36:45 25 4
gpt4 key购买 nike

我正在用 flutter 创建一个应用程序,当我想声明一个变量并在之后初始化它时,出现错误

Error: Field '_prenom' should be initialized because its type 'String' doesn't allow null.

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;
}

}

最佳答案

Dart 是 null safe .您要么必须始终分配一个值,要么使用 ?

String _prenom = "Something";

String? _prenom;

关于flutter - 错误 : Error: Field '_prenom' should be initialized because its type 'String' doesn't allow null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67877752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com