4

I have this stateless date of birth input class, read-only. When tapped, it will show a date picker and after the user picked a date, it will send the value to the bloc state, and it will change the state and the value so it shows the picked date from the date picker. This is the code:

class DateOfBirthInput extends StatelessWidget {
  DateOfBirthInput({
    required this.textEditingController,
    required this.focusNode
  });

  final TextEditingController textEditingController;
  final FocusNode focusNode;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<SignUpBloc, SignUpState>(
      buildWhen: (previous, current) => previous.dateOfBirth != current.dateOfBirth,
      builder: (context, state) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Date of Birth',
              style: TextStyle(
                color: Colors.grey[500],
                fontWeight: FontWeight.w600
              )
            ),
            TextFormField(
              enabled: state.formEnabled,
              controller: textEditingController,
              focusNode: focusNode,
              readOnly: true,
              decoration: InputDecoration(
                hintText: '1 January 1990',
                hintStyle: TextStyle(
                  color: Colors.grey[400],
                  fontWeight: FontWeight.w500
                ),
                suffixIcon: Icon(
                  Icons.calendar_today_outlined,
                  size: 24.0,
                  color: Colors.grey[500],
                ),
                errorText: state.dateOfBirthDisplay.invalid ? 'Please enter your date of birth' : null,
                errorStyle: TextStyle(
                  fontSize: 14.0,
                  color: Colors.red,
                  fontWeight: FontWeight.w500
                ),
              ),
              onTap: () async {
                DateTime? datePicker = await showDatePicker(
                  context: context,
                  initialDate: DateTime.now(),
                  firstDate: DateTime(1900, 1),
                  lastDate: DateTime.now()
                );
                if (datePicker != null) {
                  String dateOfBirth = DateFormat('dd MMMM yyyy').format(DateTime.parse(datePicker.toString()));
                  textEditingController.text = dateOfBirth;
                  context.read<SignUpBloc>.add(DateOfBirthChanged(dateOfBirthDisplay: dateOfBirth, dateOfBirth: datePicker));
                }
              }
            ),
          ],
        );
      },
    );
  }
}

In this context, i got an error. This is the context:

context.read<SignUpBloc>.add(DateOfBirthChanged(dateOfBirthDisplay: dateOfBirth, dateOfBirth: datePicker));

This is the error:

The prefix 'context' can't be used here because it is shadowed by a local declaration. Try renaming either the prefix or the local declaration.

I've tried to change one of the contexts, the builder: (context, state), I've changed to builder: (ctx, state), and still get the same error.

Please help.

2 Answers2

1

Currently, you are trying to access the SignupBloc from the signup bloc's builder context, the context variable because both of your stateless widget's context and blocbuilder's context are named as context

Rename one of your contexts to something else, either build(BuildContext context) or builder: (context, state) {

You can rename builder: (ctx, state) then the error should be gone.

krishnakumarcn
  • 3,959
  • 6
  • 39
  • 69
0

Try this change.

builder: (ctx, state) {
  //...
  ctx.read<SignUpBloc>.add(DateOfBirthChanged(dateOfBirthDisplay: dateOfBirth, dateOfBirth: datePicker));
},
Jigar Patel
  • 4,953
  • 1
  • 12
  • 20