2

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


ibex
  • 103
  • 1
  • 6

3 Answers3

4

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

Tom Rivoire
  • 627
  • 5
  • 6
1

Try removing the const for the InputDecoration()

0

You can try removing the final keyword from the string

Kaival Patel
  • 461
  • 3
  • 8
  • Yes I tried removing final keyword, but it gived error "This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final" – ibex Apr 27 '21 at 04:28