I don't know what's the problem here but i get an error on [this.reference], it says "The parameter 'reference' can't have a value of 'null' because of its type, but the implicit default value is 'null'." When I add required it says "Can't have modifier 'required' here"
Here's the code for it:
import 'package:cloud_firestore/cloud_firestore.dart';
class Request {
final String type;
final String reason;
DocumentReference reference;
Request(this.reason, this.type, [this.reference]);
String get requestId {
return reference.id;
}
Map<String, dynamic> toMap() {
return {"type": type, "reason": reason};
}
}
CodePudding user response:
class Request {
final String type;
final String reason;
DocumentReference? reference;
Request(this.reason, this.type, [this.reference]);
String get requestId {
return reference!.id;
}
Map<String, dynamic> toMap() {
return {"type": type, "reason": reason};
}
}
Guessing you are running on null-safety, you may want to make DocumentReference nullable
CodePudding user response:
Did u tried making those Nullable
final String? type;
final String? reason;
Also Try Giving default initial values
Request(String reason = '',String type = '');
CodePudding user response:
Square brackets around a parameter make that parameter an optional positional parameter. Therefore when you write:
Request(this.reason, this.type, [this.reference]);
You are specifying that the Request constructor take two required positional parameters (reason and type) and an optional positional parameter (reference).
If you want reference be optional, it must have a default value if the caller doesn't provide it. Your options are:
- Provide a
const, non-null default value:
(TheRequest( this.reason, this.type, [this.reference = DocumentReference.someConstConstructor()], );DocumentReferenceclass frompackage:cloud_firestoredoes not provide aconstconstructor, so you can't do this in your case, but I've listed it here for completeness.) - Make
referencenullable so that it can usenullas an implicit default value:
or if you want a nullable parameter with a non-nullable member:class Request { ... DocumentReference? reference; ... Request(this.reason, this.type, [this.reference]);
The latter is a general technique for making parameters optional when the parameter type has noclass Request { ... DocumentReference reference; ... Request(this.reason, this.type, [DocumentReference? reference]) : reference = reference ?? DocumentReference();constconstructors available. - Make
referencenon-optional. Since using square brackets around a parameter make that parameter optional, adding therequiredkeyword would be a contradiction and would make no sense. If you want it to be a required positional parameter, simply remove the square brackets:
Or if you want to be a required named parameter, use curly braces with theRequest(this.reason, this.type, this.reference);requiredkeyword:Required(this.reason, this.type, {required this.reference});
I suggest reading the Functions Parameters section of the Dart Language Tour.
