Is this a Dart Constructor? [duplicate] - dart

This question already has answers here:
The difference between the use of constructor " className() and className._()
(2 answers)
Closed 1 year ago.
I'm still getting used to Dart and I've searched for an answer to this, but I haven't found one. What does the second line, 'ClassA._();', of the class definition do? The list declaration is only shown to give the class a reason for being.
class ClassA {
ClassA._();
static final List<String> someList = ["A", "B", "C"];
}
If it's a constructor, how would it be invoked?
After doing some more sleuthing I see it creates a singleton. But when is the singleton created? Is there a way to get in front of the instantiation and make some mods?
Thanks in advance for any help given.

ClassA._();
Is a named constructor with the name _. In dart, identifiers that start with underscore (_) are only visible inside the library it is contained in.
The reason you would define a class this way, is that you want to prevent people from creating instances of this class.

Related

What is the use of `set` keyword during property declaration in Swift? [duplicate]

This question already has answers here:
Private setter "set()" in Swift
(3 answers)
Closed last month.
I saw this line of code in one of the classes from a project at work.
static private(set) var shared = UserPreferences()
I dont understand the use of (set) keyword in the above declaration. Tried normal googling but was unable to find anything useful. Can someone kindly explain its use? Thanks!
This is not a set keyword but a private(set) keyword.
This makes the var accessible as internal but constricts the write (set) access to private.
So this value can only be set from inside the type it is defined in. But can be read from anywhere in the module that the type is defined in.
You can read more about this here... https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID17

What's the point of prefixing the constructor call with "const" if it's already defined in the declaration with one? [duplicate]

This question already has answers here:
Const constructor create non-const class objects?
(1 answer)
Dart: Is there a disadvantage to using const constructor?
(1 answer)
Closed 1 year ago.
this might be a weak question to ask since it hasn't been long since I started using Dart.
If I got it right, a dart class constructor declared using the keyword "const" should be creating "compile time constant" objects, and those objects have only final fields and "const" declared objects, so every const object created will be using the same read-only resources defined in memory, and this would improve the performance of the app.
If I instantiate an object without the keyword "const", and using a const declared constructor, I will be getting a linting error saying that I should be prefixing the object instantiation with a "const" keyword.
So what's the point of prefixing the constructor call with "const" if it's already defined in the declaration with one?
How come an instance not provided with "const" keyword not be "compile time constant" when the constructor is already declared to be "const"?

Defining property in constructor

I'm trying to do something that would be a basic feature in any other Object Oriented Language but for some reasons in Dart, I can't manage to do it. I'm new to Dart so this question might be dumb, but I couldn't find any answer online.
I have a property that need to be calculated once and on the constructor. This is my code so far :
class Game {
String _wordChosen;
Game() {
final _random = Random();
_wordChosen = WORDS[_random.nextInt(WORDS.length)];
}
}
WORDS is a list defined outside the class. My error is on the Game constructor :
not_initialized_non_nullable_instance_field.
I don't want to set the _wordChosen variable to a default value as that would make no sense (it would be overwritten right when the constructor is run).
I also don't want to set the property as nullable as again, it would make no sense.
i think the answer is using the keyword late to make compiler know that you will initialize the variable before using it but not now like below
late String _wordChosen;
i think this is your solution and it in null safety documents here
i hope this answer helps you

What's mean colon inside a dart class?

I'm trying understand what's the mean of this two final lines of code, on colons... It's a syntax question.
I'm following this github example and I have this doubt on my head.
Can someone help me with this?.
class DietPlan extends ParseObject implements ParseCloneable {
DietPlan() : super(keyDietPlan);
DietPlan.clone() : this();
The part after : is called "initializer list.
It is a list of expressions that can access constructor parameters and can assign to instance fields, even final instance fields.
The first colon, i.e. DietPlan() : super(keyDietPlan); means that you are calling the super constructor, constructor of ParseCloneable in this case.
This is a core OOP concept, you can extend or implement one class to another and you must call the superclass constructor if you do so. This is just a style of doing the same in Dart.
The second colon works in similar way, to understand that you need to understand what is cloning of objects,
Object cloning refers to creation of exact copy of an object. It creates a new instance of the class of current object and initializes all its fields with exactly the contents of the corresponding fields of this object.
This is whats happening on the second line.

How to write a function type in Dart [duplicate]

This question already has answers here:
What are function typedefs / function-type aliases in Dart?
(7 answers)
Closed 4 years ago.
This seems like a very obvious question to me, but I cannot seem to find the answer.
How do I write a function type in Dart.
I can use some canned forms like ValueChanged<T> or ValueSetter<T>, but how are they defined? How can I write general function types like T -> U -> V
If I understood your question correctly, I think you are referring to Generic Types. These types are used by functions when you are not sure which type your function is going to use in order to avoid code duplication. For example, let's say you have a class with a method that stores numbers in a List:
class StoreInt {
storeInList(int value);
}
But later, you decide you now want it to store Strings, so you create another class:
class StoreString {
storeInList(String value);
}
Generics allow you to write this class once and use it to store any kind of object you want:
class Store<T> {
storeInList(T value);
}
As far as I know, you don't create generics in dart, you just specify a single letter in between diamond operators as shown in the previous example.
You can find more information on generics here: https://www.dartlang.org/guides/language/language-tour#generics

Resources