nxmo
nxmo

Reputation: 15

this.reference is not working on flutter/dart

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};
  }
}

Upvotes: 0

Views: 632

Answers (3)

jamesdlin
jamesdlin

Reputation: 89956

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:
    Request(
       this.reason,
       this.type,
       [this.reference = DocumentReference.someConstConstructor()],
    );
    
    (The DocumentReference class from package:cloud_firestore does not provide a const constructor, so you can't do this in your case, but I've listed it here for completeness.)
  • Make reference nullable so that it can use null as an implicit default value:
    class Request {
      ...
      DocumentReference? reference;
      ...
      Request(this.reason, this.type, [this.reference]);
    
    or if you want a nullable parameter with a non-nullable member:
    class Request {
      ...
      DocumentReference reference;
      ...
      Request(this.reason, this.type, [DocumentReference? reference])
        : reference = reference ?? DocumentReference();
    
    The latter is a general technique for making parameters optional when the parameter type has no const constructors available.
  • Make reference non-optional. Since using square brackets around a parameter make that parameter optional, adding the required keyword would be a contradiction and would make no sense. If you want it to be a required positional parameter, simply remove the square brackets:
    Request(this.reason, this.type, this.reference);
    
    Or if you want to be a required named parameter, use curly braces with the required keyword:
    Required(this.reason, this.type, {required this.reference});
    

I suggest reading the Functions Parameters section of the Dart Language Tour.

Upvotes: 0

Aravind Aji
Aravind Aji

Reputation: 154

Did u tried making those Nullable

final String? type;
final String? reason;

Also Try Giving default initial values

Request(String reason = '',String type = '');

Upvotes: 0

Tim
Tim

Reputation: 172

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

Upvotes: 1

Related Questions