【问题标题】:Bad: Error: Field '_prenom' should be initialized because its type 'String' doesn't allow null坏:错误:字段“_prenom”应该被初始化,因为它的类型“字符串”不允许为空
【发布时间】: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;
}

}

谢谢你的回答...

【问题讨论】:

标签: flutter dart


【解决方案1】:

在 Dart 类中总是添加 ?在数据类型之后

bool? log = true;
String? mail;
String? password;
String? prenom;
String? nom;```

【讨论】:

    【解决方案2】:

    飞镖是 null safe 。您要么必须始终分配一个值,要么使用 ? 将其显式标记为可为空

    String _prenom = "Something";
    

    或

    String? _prenom;
    

    【讨论】:

    • 感谢 ? 可以正常工作,但我不知道为什么,但它标记了字段“_prenom”的值未使用。但这不是问题谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多