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

Viewed 1986

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.

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) {
  },
);
  }
}


3 Answers

The error comes from the fact of using a variable widget.hint inside of const object InputDecoration

I can't find anywhere in the date_field code where it forces you to use a constant decoration

So you might just remove the const keyword in front of InputDecoration

See this answer for details about the difference between const and final

Try removing the const for the InputDecoration()

You can try removing the final keyword from the string

Related