Asked  4 Months ago    Answers:  5   Viewed   1.7k times

Ive been trying to get this right for some time and can't figure out what is wrong.

enum MyEnum { a, b }

class ClassA {
  final MyEnum myEnum;
  ClassA({this.myEnum = MyEnum.a});
}

class ClassB {
  final ClassA classA;
  ClassB({this.classA = ClassA()}); // ClassA expression is underlined with red
}

The IDE (Visual Studio Code) complains with:

[dart] Default values of an optional parameter must be constant.

Ive tried to prefix it with const, new, and passing values to the ClassA constructor, but nothing works. Can anyone see what I am doing wrong here?

cheers

 Answers

16

Try

enum MyEnum { a, b }

class ClassA {
  final MyEnum myEnum;
  ClassA({this.myEnum});
}

class ClassB {
  final ClassA classA;
  ClassB({this.classA}); // ClassA expression is underlined with red
}

no need for '=' operator. It will automatically assign the value when you will pass it to the constructor.

Use the '=' operator only when you need to pass a default value to your variables hence, making them optional parameters.

Edit

enum MyEnum { a, b }

class ClassA {
  final MyEnum myEnum;
  const ClassA({this.myEnum = MyEnum.a});
}

class ClassB {
  final ClassA classA;
  ClassB({this.classA = const classA()}); // ClassA expression is underlined with red
}

This is the only way i could find to achieve what you want, the constructor should be default

This is called a canonicalized constructor.

Friday, August 13, 2021
 
Awais Qarni
answered 4 Months ago
49

You can't create compile-time constants of object references.

The only valid compile-time constant you can use is null, so change your code to this:

public void DoSomething(int index, ushort[] array = null,
  bool thirdParam = true)

And inside your method do this:

array = array ?? new ushort[0];

(from comments) From C# 8 onwards you can also use the shorter syntax:

array ??= new ushort[0];
Wednesday, June 2, 2021
 
rblarsen
answered 7 Months ago
74

No, the expression PersonRespository.ColumnID is not classified as a compile-time constant. The expression "ID" is, but that's not what you're using as the default parameter.

In particular, if ColumnID is "just a normal field" then any references to it will be resolved as a field - so if you compile an assembly which refers to the field, then change the value and rebuild the assembly containing PersonRepository, the referring assembly will see that change.

If you change your declaration to:

 public const string ColumnID = "ID";

then it is a compile-time constant expression. That means in our previous scenario, the value of the constant is baked into any code that refers to it - and changing the value later without recompiling that referring code won't change the value used by that referring code.

See section 7.19 of the C# 4 language specification for more details about what counts as a constant expression.

Friday, June 25, 2021
 
Sujith
answered 6 Months ago
89

Once Access-Control-Allow-Origin: * header has been added to every response of your REST API (don't forget OPTIONS request - see perlight request), you should be able to use it without problem with XMLHttpRequest and debug as it was on the same server.

Under development you can also use --disable-web-security option to launch Dartium ( see Disable same origin policy in Chrome )

Sunday, August 15, 2021
 
hohner
answered 4 Months ago
90

For cookies being sent to CORS requests, you need to set withCredentials = true. The browser client in the http package doesn't support this argument. You can use the HttpRequest from dart:html instead. See How to use dart-protobuf for an example.

Thursday, August 19, 2021
 
Jason
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share