0
import 'dart:typed_data';
class Test {
  Uint8List testKey;
  Test({this.testKey = Uint8List(0)});
}

Gives compile error: Error: Cannot invoke a non-'const' factory where a const expression is expected.

Also tried -- Uint8List.fromList([]); with same result.

How can I do this? Thanks

1 Answer 1

0

In Dart, a default parameter value must be a compile-time constant. Since the class Uint8List does not provide a const constructor, your default value could be a constant object of type List<int> instead:

import 'dart:typed_data';

class Test {
  Uint8List testKey;
  Test({List<int> inputList = const []})  // Note the keyword "const".
    : testKey = Uint8List.fromList(inputList);
}
Sign up to request clarification or add additional context in comments.

1 Comment

that worked! Thanks - don't know how long it would have taken to figure that out! Maybe suggest that all the definitions like Uint8List have a static, const named 'empty' for situations like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.