When to use the "new" keyword in a Dart widget [duplicate] - dart

This question already has answers here:
Do you need to use the "new" keyword in Dart?
(3 answers)
Closed 3 years ago.
When, for example, I want to create a button with the text "button" in it I create a RaisedButton with a Text widget as its child. Wheter or not I use the 'new' keyword before this Text does not seem to make a difference in the end result of the button. Therefore I was wondering in what situation the new keyword would be necessary when making use of the regular widget like RaisedButton and Text.
I am, off course, familiar with the 'new' keyword and its uses to create a new object but in the situation that I just described I do not understand how the keyword makes a difference and wheter or not I should use it.

The new keyword is totally optional from Dart 2.0. It's not mandatory to use it.

Related

Is this a Dart Constructor? [duplicate]

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.

Replacement for actiomessages class in Struts 2 [duplicate]

This question already has an answer here:
Migration error handling to Struts2
(1 answer)
Closed 2 years ago.
I am trying to update my code from Struts1 to Struts 2 but facing difficulty to update the below code can any one please suggest the replacement for below code.
final ActionMessages errors=new ActionMessages();
errors.add("Error Global", new ActionMessage("some_string_in_properties_file"));
sorterrormessage(errors);
Extending ActionSupport provides the addActionMessage method. It also provides getActionMessages and setActionMessages (Collection<String>) which would allow sorting them. 1
The tag library provides <s:actionmessage />, when combined with hasActionMessages allows checking for, and displaying, the action messages.
To use a property file use an appropriate getText() implementation in the action.
It would be good to know what difficulties you're actually facing as this is searchable on the web and it's in the S2 docs. A cursory examination of the functionality in ActionSupport would probably be a good idea.
1 Sorting messages is only rarely the right thing to do except under very particular circumstances, or if it's explicitly not an alphabetical sort.

What's the difference between "Text" and "new Text" in dart?

What's the difference between Text and new Text in dart?
Also suitable for Container and all other widgets.
As of Dart 2, you don't need to use new keyword to create a new instance. So basically, if you're developing in Flutter and Dart 2+, Text and new Text is the same.
Take a look at the Dart documentation about constructors.
In Dart 2 new is an optional keyword. So Text and new Text is basically the same thing.

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

Dynamically reference const (Objective-C) [duplicate]

This question already has an answer here:
How do I lookup a string constant at runtime in Objective-C?
(1 answer)
Closed 8 years ago.
Is it possible to dynamically reference a constant in objective c:
e.g. NSForegroundColorAttributeName is declared in UIKit/NSAttributedString.h as a const NSString. Its value is #"NSColor". In compiled code, I just specify the constant to use it, but what if I need to refer to it dynamically at run time (say a user might type it out)?
I could just use #"NSColor", but that may change as the SDK changes.
Is there a function like NSClassFromString, say ConstFromString:
ConstFromString(#"NSForegroundColorAttributeName") --> #"NSColor"
If the symbol is a public, exported symbol, then you might be able to look it up at runtime using dlsym and friends, but I agree with the commenter that this sounds like a bad idea.

Resources