1

How to use @required annotion in abstract class? There is no error on SubjectVM.dart. I want it to be @required automatic

import 'package:flutter/material.dart';
import 'package:todo/models/subject.dart';

abstract class SubjectBase {
  void addSubject(Subject value);
  void deleteSubject(Subject value);
  void deleteSubjectByIndex(int index);
  void updateSubject({@required Subject updatedSubject, @required int indexToReplace});
}

SubjectVM.dart

class SubjectVM implements SubjectBase {
  List<Subject> listOfSubjects = <Subject>[];

  @override
  void addSubject(Subject value) {
    // TODO: implement addSubject
  }

  @override
  void deleteSubject(Subject value) {
    // TODO: implement deleteSubject
  }

  @override
  void deleteSubjectByIndex(int index) {
    // TODO: implement deleteSubjectByIndex
  }

  @override
  void updateSubject({Subject updatedSubject, int indexToReplace}) {
    // TODO: implement updateSubject
  }

}

There is no error message for SubjectVM. I can use without parameters, but i dont want it

SubjectVM subjectVM =  SubjectVM() 
subjectVM.updateSubject();

I want use it with @reqiured named parameters.I can do this if i add to @required annotions to SubjectVM.updateSubject. But its manual way.Any suggestions?

763
  • 1,543
  • 1
  • 12
  • 15

1 Answers1

2

Your @required annotations are related to SubjectBase class, not to SubjectVM. If you need these annotations to work, you need to execute a method of SubjectBase. This class is abstract and doesn't have an implementation, but you can use the implementation of a child.

Try this:

SubjectBase subjectVM =  SubjectVM();
subjectVM.updateSubject();
  • Thanks for reply but its not work and i dont want use this method. I can use subjectVM.updateSubject(); without error . But its wrong,I want use it with named parameters and this named parameters are required. – 763 May 06 '20 at 19:04
  • In my example, method updateSubject from SubjectVM is using with errors from SubjectBase, and not necessary to add annotations one more time, as you requested. If I understood your request correctly. If you run **flutter analyze**, with my example, you will get errors: _"The parameter 'indexToReplace' is required"_ and _"The parameter 'updateSubject' is required"_, and if you execute code, method SubjectVM.updateSubject will be executed – Aleksandr Denisov May 07 '20 at 05:55