Home > Blockchain >  Dart thinks variable is marked final when it is not
Dart thinks variable is marked final when it is not

Time:10-21

I'm getting this error:

The final variable 'hasFocus' can only be set once. Try making 'hasFocus' non-final.dartassignment_to_final_local

I'm getting the error on this line here hasFocus = isFocused;

Code below shows hasFocus is not final, so why the error?

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:vepo/src/presentation/common/constants/length.dart';
import 'package:vepo/src/presentation/widgets/form_fields/date_time_range_field/date_time_range_picker_field.dart';
import 'package:vepo/src/presentation/widgets/form_fields/duration_field/duration_field_vm.dart';
import 'package:vepo/src/presentation/widgets/interactive_non_form_widgets/buttons/icon_buttons/icon_button/icon_button_widget.dart';

class VpDurationField extends ReactiveFormField<Duration, Duration> {
  VpDurationField(
      {required this.formControlName,
      required this.context,
      required this.hintText,
      this.labelText,
      this.textColor,
      this.hasFocus = false,
      this.textAlign = TextAlign.left,
      this.form,
      this.onSubmitted,
      this.key,
      this.textInputAction = TextInputAction.done,
      this.nextField,
      this.validationMessages,
      this.onFocusChange})
      : super(
            key: key,
            formControlName: formControlName,
            validationMessages: validationMessages,
            showErrors: (control) => control.invalid,
            builder: (ReactiveFormFieldState<Duration, Duration> field) {
              final vm = context.read(durationFieldVmProvider);

              return Container(
                  height: LENGTH_7,
                  child: FocusScope(
                      child: Focus(
                    onFocusChange: (isFocused) {
                      hasFocus = isFocused;
                      if (onFocusChange != null) {
                        onFocusChange(isFocused);
                      }
                    },
                    child: TextField(
                      controller: vm.textEditingController,
                      onTap: () => vm.showDurationField(context),
                      keyboardType: TextInputType.datetime,
                      enableInteractiveSelection: false,
                      focusNode: AlwaysDisabledFocusNode(),
                      decoration: InputDecoration(
                          hintText: hintText,
                          errorMaxLines: 3,
                          suffixIconConstraints: const BoxConstraints(
                            minWidth: 2,
                            minHeight: 2,
                          ),
                          suffixIcon: VpIconButton(
                              icon: Icon(
                            FontAwesomeIcons.calendarPlus,
                            color: Theme.of(context).colorScheme.primary,
                          )),
                          alignLabelWithHint: true,
                          labelStyle: TextStyle(
                              height: 0,
                              fontSize: 18.0,
                              color:
                                  Theme.of(context).colorScheme.primaryVariant),
                          labelText: labelText,
                          counterText: ''),
                    ),
                  )));
            });

  final TextAlign textAlign;
  final String hintText;
  final String formControlName;
  final String? labelText;
  final void Function(bool)? onFocusChange;
  final void Function()? onSubmitted;
  final Color? textColor;
  final BuildContext context;
  bool hasFocus;
  Map<String, String> Function(AbstractControl<Duration>)? validationMessages;

  /// This is only required when setting the next field to focus on.
  final FormGroup? form;
  Key? key;

  /// This is only required when setting the next field to focus on.
  final String? nextField;

  /// This is to be 'next' if not the last field, and 'done' when is the last field.
  final TextInputAction textInputAction;
}

CodePudding user response:

Try removing

this.hasFocus = false

CodePudding user response:

in my opinion it's better to create another newVariable and let this newVariable = hasFocus instate of use hasFocus use newVariable.

CodePudding user response:

I am not getting any such error while trying a similar code. Maybe try

  1. Restarting your IDE
  2. Check if you are using latest flutter & dart version
  • Related