必须向文本小部件提供非空字符串。失败的断言:第 378 行 pos 10:'data != null'

时间:2021-04-14 09:30:39

标签: flutter dart

嗨,我遇到了一个错误,我尝试查看“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;
                              });
                            },
                            ),
                            )
                          ],
                        ),

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

>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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