简体   繁体   English

为什么我收到错误,flutter statfulwidget flutter 中的常量值无效

[英]Why I'm getting the error, Invalid constant value in flutter statfulwidget flutter

In the below code widget.hintText is giving the error, I am trying to make the datepicker as the seperate component and dynamically pass the hinttext value whenever calling it from the other file.在下面的代码中,widget.hintText 给出了错误,我试图将 datepicker 作为单独的组件,并在从另一个文件调用它时动态传递提示文本值。

import 'package:date_field/date_field.dart';
import 'package:flutter/material.dart';


class DatePicker extends StatefulWidget {
  final String hintText;
  DatePicker({
    this.hintText,
    Key key,
  }): super(key: key);

  @override
  _DatePickerState createState() => _DatePickerState();
}
class _DatePickerState extends State<DatePicker> {
  @override
  Widget build(BuildContext context) {
    return DateTimeFormField(
  decoration: const InputDecoration(
    hintText: widget.hintText,
    hintStyle: TextStyle(color: Colors.black54,fontSize: 16),
    errorStyle: TextStyle(color: Colors.redAccent),
    suffixIcon: Icon(Icons.event_note),
  ),
  mode: DateTimeFieldPickerMode.date,
  autovalidateMode: AutovalidateMode.always,
  // validator: (e) => (e?.day ?? 0) == 1 ? 'Please not the first day' : null,
  onDateSelected: (DateTime value) {
  },
);
  }
}


The error comes from the fact of using a variable widget.hint inside of const object InputDecoration该错误来自在 const object InputDecoration内部使用变量widget.hint的事实

I can't find anywhere in the date_field code where it forces you to use a constant decoration我在date_field代码中找不到强制您使用常量decoration的任何地方

So you might just remove the const keyword in front of InputDecoration所以你可能只是删除InputDecoration前面的const关键字

See this answer for details about the difference between const and final有关constfinal之间区别的详细信息,请参阅此答案

You can try removing the final keyword from the string您可以尝试从字符串中删除final关键字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM