beginner in Flutter here,
Does anybody know the difference, or significance of the keyword get in the context of instantiating a widget?
I'm declaring a widget here
ListTile sampleListTile {
return ListTile(...);
}
In this scenario, I'm getting an error of Methods must have an explicit list of parameters., to be solved by coding it as a method by adding () to the widgetname.
But here,
ListTile get sampleListTile {
return ListTile(...);
}
Everything works out alright... I tried searching around but I can't nail down the relevant article.
Please help. thanks.
Your first syntax is an "instance method", so the syntax should be (note the extra ()):
ListTile sampleListTile() {
return ListTile(...);
}
which you call like:
ListTile foo = sampleListTile();
and can have parameters as necessary.
The second syntax is of a getter which are special instance methods that have no parameters. They feel more like "instance variables" in that you use them like:
ListTile bar = sampleListTile;
Related
I've got a method that does the default approach when an error occurs and simply dumps the error to the user's computer screen:
void onError(FlutterErrorDetails details) => FlutterError.dumpErrorToConsole(details);
Now, the user is invited to override this method, of course, but when the time comes and an error does occur, I want to know if the method was overridden or not. I can't figure out how one would do this however.
In Dart, functions are first-class objects, and so is there not a way to 'record' what the method was as the class defines it, and what it is now when the class object is instantiated? Something like:
static final FlutterExceptionHandler _dumpError = (FlutterErrorDetails details) => FlutterError.dumpErrorToConsole(details);
if (onError == _dumpError) {
_prevOnError(details);
} else {
onError(details);
}
Now the above doesn't work, of course. onError does not equal _dumpError. I even tried this:
static final FlutterExceptionHandler _dumpError = FlutterError.dumpErrorToConsole(details);
void onError(FlutterErrorDetails details) => _dumpError;
Now this actually works in that, when an error occurs, onError() fires and dumps the error to the console. However, it's not quite right as the comparison (==) would, again, return false. How do I make it true?: onError == _dumpError
Is there a way to do this, you think? Is there a way to compare the 'contents' of two functions??
I dunno. I'm grasping at straws at this point.
Cheers.
"I want to know if the method was overridden or not. I can't figure out how one would do this however". The point of OOP is that you shouldn't ever need to care, or even know, if that had happened. What's your actual use case?
How come in dart in a line of code such as this one:
MaterialPageRouter(builder: (context) => MyWidget())
We are returning MyWidget class with out instantiating it with the keyword new as in new MyWidget() ? Are we just returning the class itself and something happens under the hood that uses the new keyword to do whats required. Or something else is happening?
new became optional in Dart 2. You can just omit it or write it. It doesn't make a difference.
MyWidget() creates a new instances and this is what is returned.
Why do some core taglibs in Grails return a closure? For example createLink (see source)? What are the benefits or use cases?
So, what you are actually seeing is that createLink is defined as a Closure, not that it's returning a Closure when it's executed. The closure itself is executed and delegates the implementation which is responsible for the actual work of creating the StreamCharBuffer.
Let's look at the source and see what's really going on:
Closure createLink = { attrs ->
return doCreateLink(attrs instanceof Map ? (Map) attrs : Collections.emptyMap())
}
As you can see above we have a variable called createLink of type Closure which delegates it's work to doCreateLink. Which happens to be a protected method within the containing class.
protected String doCreateLink(Map attrs) {
... // actual implementation cut out of this example
return useJsessionId ? res.encodeURL(generatedLink) : generatedLink
}
As you can see this is where the actual work is done to generate the StreamCharBuffer (Well, String, which casts nicely).
Now, why would you do this? One possible use case is, the method doCreateLink is much more strict than the closure createLink in the formal definition. By using a Closure instead of the method, the call to createLink can change slightly over time as enhancements or additions are made to it with (hopefully) little or no impact to previous uses of it.
Hopefully that helps explain a bit about what you are seeing and possibly why.
The reason it took me forever to find out is that I don't know how it is called. But I hope if I describe the question here as thoroughly as possible, it will pop up in the search results anyways.
Possible other titles:
How can I pass a function without name as parameter to querySelector.onClick.listen?
How to use a function without a name in dart?
How to create a nameless function that you can use only exactly there where it is defined?
I know that the following works - also hosted on dartpad
void main(){ querySelector('#btn').onClick.listen((e)=>fnct()); }
void fnct(){ querySelector('#btn').text="hola"; print("test");}
This changes the text of the button to "hola" and prints "test".
But what if I don't want to define a new function just for this because I like to keep the flow when reading the code and don't like to jump from function to function needlessly?
After an hour of searching, I found this
For my own code example, it would be like this dartpad link:
void main(){
querySelector('#btn').onClick.listen((e){
querySelector('#btn').text="hello";
print("no hablo espanol");
});
}
So you can define a function on the flow by using
(param){command(); secondCommand(param);}
It is entirely possible that you can find this somewhere. But I did not with my search terms. So if any of you know what the correct search terms would have been, let me know :)
Taking the below method as an example:
-(void)myMethodName:(int)xCoordinate yCoordinate:(int)yCoordinate
When I write the method in my code [self etc...], Xcode does its auto complete which tells me what the second value is for but never the first (see pic).
Is there any way I can also include the 'description' for the first value of a method?
This is just a matter of how you name your method. It autocompletes the entire method name. If the method name is descriptive of its parameters, then you'll see it in the autocomplete.
Are you trying to get something like this?
-(void)myMethodNameWithXCoordinate:(int)xCoordinate yCoordinate:(int)yCoordinate
This is how the methods are usually defined in objective C
- (int)addX:(int)x toY:(int)y {
int sum = x + y;
return sum;
}
Like others have said rename your method this way and it will make things clearer
-(void)moveStuffFromXCoordinate:(int)xCoordinate toYCoordinate:(int)yCoordinate
Based on the Apple Documentation on coding guidelines for cocoa when naming methods with parameters the word before the argument should describe the argument.
Make the word before the argument describe the argument.
Right - (id)viewWithTag:(NSInteger)aTag;
The above is right because it describes the argument as a Tag where as the below doesn't describe the argument as anything.
Wrong - (id)taggedView:(int)aTag;
So in your case
Wrong -(void)myMethodName:(int)xCoordinate yCoordinate:(int)yCoordinate;
it should be
Right -(void)myMethodNameForXCoordinates:(int)xCoordinate andYCoordinate:(int)yCoordinate;