【问题标题】:A non-null String must be provided to a Text widget .Failed assertion: line 378 pos 10: 'data != null'必须向 Text 小部件提供非 null 字符串。断言失败:第 378 行 pos 10: 'data != null'
【发布时间】:2021-04-14 09:30:39
【问题描述】:

您好,我收到一个错误,我尝试查看“null(s)”,但不确定是哪一个引发了错误。

代码:

class AssessmentForm extends StatefulWidget {
  @override
  AssessmentFormState createState() => AssessmentFormState();
}
class AssessmentFormState extends State<AssessmentForm> {

  ValueNotifier selectFacility=ValueNotifier(null);
  ValueNotifier selectCommunity=ValueNotifier(null);

  final GlobalKey<FormState> _formKey = GlobalKey();
@override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        centerTitle: true,
        iconTheme: IconThemeData(color: AppColor.primarypurple),
        title: Text(
          getTranslated(context,'Assessment Form'),
          style: TextStyle(
              color: AppColor.primarypurple,
              fontSize: 20,
              fontWeight: FontWeight.bold),
        ),
        actions: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: DropdownButton<Language>(
              underline: SizedBox(),
              icon: Icon(
                Icons.language,
                color: Colors.white,
              ),
              onChanged: (Language language) {
                _changeLanguage(language);
              },
              items: Language.languageList()
                  .map<DropdownMenuItem<Language>>(
                    (lang) => DropdownMenuItem<Language>(
                  value: lang,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Text(
                        lang.flag,
                        style: TextStyle(fontSize: 30),
                      ),
                      Text(lang.name)
                    ],
                  ),
                ),
              )
                  .toList(),
            ),
          ),
        ],
      ),
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(colors: [
            Color.fromRGBO(4, 131, 187, 0.21),
            Color.fromRGBO(210, 74, 187, 0.6),
            Color.fromRGBO(0, 132, 187, 0.2),
          ], begin: Alignment.bottomLeft, end: Alignment.topRight),
        ),
        padding: const EdgeInsets.only(top: 100),
        child: Stack(
          children: [
            RegisterShape(),
            Container(
              padding: EdgeInsets.only(top: 120),
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.only(
                      left: 20.0, right: 20, bottom: 10, top: 10),
                  child: Form(
                    key: _formKey,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          getTranslated(context,'Current Site'),
                          style:TextStyle(fontSize: 20,fontWeight:FontWeight.bold),),
                        Row(
                          children: [
                            Expanded(child:
                            CheckboxListTile(
                              title:Text(
                                  getTranslated(context,'Facility'),
                                  style: TextStyle(
                                      fontSize:14
                                  )),
                              controlAffinity: ListTileControlAffinity.leading, value: firstCheck, onChanged: (bool value) {
                              setState(() {
                                firstCheck = value;
                                secondCheck = !value;
                              });
                            },
                            ),
                            ),
                            Expanded(child:
                            CheckboxListTile(
                              title: Text(
                                  getTranslated(context, 'Community'),
                                  style: TextStyle(
                                      fontSize:14
                                  )),
                              controlAffinity: ListTileControlAffinity.leading, value: secondCheck, onChanged: (bool value) {
                              setState(() {
                                firstCheck = !value;
                                secondCheck =value;
                              });
                            },
                            ),
                            )
                          ],
                        ),

我通过标题中的一个小部件传递一个空值,这就是它给出错误的原因。如果这样说我传递一个空值,我如何使用和传递我的标题中不为空的值

【问题讨论】:

  • 只要先检查该值是否为null。如果是,不要使用Text 或将值初始化为空字符串。
  • @moneer alhashim 这是导致错误的行 title: Text( getTranslated(context,'Facility'), style: TextStyle( fontSize:14 )), – Rishail Siddiqui 12 分钟前 删除

标签: flutter dart


【解决方案1】:

因为 getTranslated() 返回 null。 尝试:

title: Text(
       getTranslated(context, 'Community')?? "null",
       style: TextStyle(
            fontSize:14
       )
),

【讨论】:

    【解决方案2】:

    由于很难看到第 378 行的确切位置,请再次查看所有文本小部件,看看您输入的文本是否可能为空,即没有值。

    【讨论】:

    • 这是第 378 行:title: Text(getTranslated(context,'Facility'), style: TextStyle(fontSize:14)),
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2023-03-24
    • 2020-12-15
    • 1970-01-01
    • 2020-11-21
    • 2020-11-18
    • 2021-04-06
    • 2020-07-04
    相关资源
    最近更新 更多