Search code examples
flutterdartoverridingwarningsinherited

how can I fix this weak warning? "The field doesn’t override an inherited getter or setter."


It shows this: "The field doesn’t override an inherited getter or setter." And because of this issue I can't push my Code in Git as it fails on test and analyze.

Any suggestions on how I can solve it? I shifted _startDate and _endDate out of Override but now it shows: "The method doesn’t override an inherited method." for displayDatePicker.

import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:date_range_picker/date_range_picker.dart' as DateRagePicker;

class Datepicker extends StatefulWidget {
  @override
  _DatepickerState createState() => _DatepickerState();
}

class _DatepickerState extends State<Datepicker> {

  DateTime _startDate = DateTime.now();
  DateTime _endDate = DateTime.now().add(Duration(days: 7));
  @override

  Future displayDatePicker(BuildContext context) async {
    {
      final List<DateTime> picked = await DateRagePicker.showDatePicker(
          context: context,
          initialFirstDate: _startDate,
          initialLastDate: _endDate,
          firstDate: new DateTime(DateTime.now().year),
          lastDate: new DateTime(DateTime.now().year + 2));
      if (picked != null && picked.length == 2) {
        setState(() {
          _startDate = picked[0];
          _endDate = picked[1];
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          RaisedButton(
            child: Text('select Date'),
            onPressed: () async {
              await displayDatePicker(context);
            },
            color: Colors.red,
            textColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(18.0),
                side: BorderSide(color: Colors.red)),
          ),
          //Text("from:${_startDate}"),
          //Text("until:${_endDate}"),
          Text(
              "from:${DateFormat('MM/dd/yyyy').format(_startDate).toString()}"),
          Text("until:${DateFormat('MM/dd/yyyy').format(_endDate).toString()}")
        ],
      ),
    );
  }
}

Solution

  • displayDatePicker is not a method of State<Datepicker>, thus, we just need to remove the @override line above Future displayDatePicker:

      // remove this line ---> @override
    
      Future displayDatePicker(BuildContext context) async {
        {
          final List<DateTime> picked = await DateRagePicker.showDatePicker(
              context: context,
              initialFirstDate: _startDate,
              initialLastDate: _endDate,
              firstDate: new DateTime(DateTime.now().year),
              lastDate: new DateTime(DateTime.now().year + 2));
          if (picked != null && picked.length == 2) {
            setState(() {
              _startDate = picked[0];
              _endDate = picked[1];
            });
          }
        }
      }