Dart Abstract Class Method with @required optimal parameters
895
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();
Author by
763
Updated on December 20, 2022Comments
-
763 over 1 year
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 about 4 yearsThanks 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.
-
Aleksandr Denisov about 4 yearsIn 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