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 :)
Related
So I have added code into a Dart Application that runs a check to see if a response is authorized and I have set it up so there is a simple 3 line bit of code that is needed in the method to check. I'm worried though that if I am not the one programming it how can we ensure that those lines get added. The code looks like below
#Operation.get()
Future<Response> returnGroupFromRegistrationCode(
#Bind.query('code') String code,
) async {
final result = await getRegistrationCodeGroupResult(context, code);
final unauthorizedResponse = result.unauthorizedResponse;
if (unauthorizedResponse != null) {
return unauthorizedResponse;
}
The final unathorizedResponse are the 3 lines that need to be added. The logical way is to add this check in the getRegistrationCodeGroupResult but I can't return the method early without a null check. Is there an error I could throw if and API is being used correctly because I didn't see an applicable one on the list? Just looking for potential ways to improve the code and make it easier to use in the future, any ideas welcome!
A few things I could hope for.
an error that could work to be thrown if the API is being used incorrectly
a way to cut off a function early without the null check
a way to possible integrate a standard into Lint or some autoformat or a scripted way to check if the lines of code are included
Say, I have:
var buffer = StringBuffer();
buffer.toString(); // works (understandable)
buffer.write('foo').toString(); // fails because `write` returns `void` (understandable)
buffer..write('bar').toString(); // but why does it fail?
You can see buffer..write('bar') returns a StringBuffer instance, and I should be able to call toString() on that instance. But why it doesn't work that way.
PS: I know I can use buffer..write('bar')..toString() to make it work but my question is not how to make that work rather I want to know the reason why it didn't work.
Because that is how the cascade operator is suppose to work.
buffer..write('bar').toString();
Is equal to do:
buffer.write('bar').toString();
Where:
buffer..write('bar')..toString();
Is equal to:
buffer.write('bar');
buffer.toString();
What you can do to make your example works is to add bracket like this so we changes the order of how each part is linked:
(buffer..write('bar')).toString();
Also found this answer which also gives some details about this behavior:
How do method cascades work exactly in dart?
I've seen this function in the "Animal Network" example of the "Composer UI Playground". It seems that the 'findAnimalsByOwnerIdWithDetails' function is never called and thus the 'query' function is never executed in that example.
function findAnimalsByOwnerIdWithDetails(farmerId)
{
return query('select resolve(a, a.location, a.owner) from Animal a where a.owner == :farmerId');
}
I've tried to incorporate the 'query' function into my own code but the run-time execution engine complains about an "unknown function". The API description also doesn't tell anything about it.
Can it be used in some way to query/manipulate the assets (and thus the ledger) or if not, then do such plans exist? -- Thx.
That functionality is not yet implemented in the runtime. The user story for that is here:
https://github.com/fabric-composer/fabric-composer/issues/67
We are holding off implementing this story until we finish the port to HL v1, which includes CouchDB; supporting much more powerful queries over world state.
In some cases, I would like to add contextual information to a message (for instance currently authenticated user), without having to include it in the message template.
I would like to accomplish this :
logger.Information("Doing stuff {Foo} with the thing {Bar}. {User}", foo, bar, user)
but without the {User} in the template.
I already know about LogContext but that seems overkill when adding contextual information to just one event.
I also know I can use the low-level API logger.Write(LogEvent evnt) to actually control which properties are included, but that seems like a bit too much code for what I am trying to accomplish.
I'm pretty sure there is a short and elegant way that is super obvious, but I haven't found it :)
UPDATE :
I found out only afterwards that this question is more or less similar : Add custom properties to Serilog
I could figure it out on my own!
You can use the fluent method .ForContext(propertyName, propertyValue) on a single call to one of the .LogXXX() methods.
For instance :
logger.ForContext("User", user)
.Information("Doing stuff {Foo} with the thing {Bar}", foo, bar)
The property added to the event only apply to the event, and they are no longer present on the next call to the logger.LogXXX() method
UPDATE
The article by Nicholas Blumhardt explains it quite well : Context and correlation – structured logging concepts in .NET (5)
If you're using the generic Microsoft ILogger you can use BeginScope;
using (_logger.BeginScope(new Dictionary<string, object> { { "LogEventType", logEventType }, { "UserName", userName } }))
{
_logger.LogInformation(message, args);
}
This is discussed here; https://blog.rsuter.com/logging-with-ilogger-recommendations-and-best-practices/
In swift i was messing around with some functions in the playground and figured out what I needed to do. So i went ahead and pasted this function into my viewController.swift file however when I went to assign a variable it did this:
(incase the images goes)
This is what autocomplete suggests when I call my function which has 5 arugments in my viewController.swift
let test = template(<#ViewController#>)
instead of this which is what it was doing in the playground
(incase images goes)
This is what the autocomplete suggests when i call it in the playground (each object between the <> i can just tab through and change)
let test = template(<question: String>, <answerOne: String>, <answerTwo: String>, <answerThree: String>, <answerFour: String>, <correctAnswer: Int>)
The only reason I ask is because it was so much easier in the playground because I could easily just hit enter and then tab through each value that needed adding, now it takes much longer, especially when I have to do it 500+ times. Is there something I am doing wrong or anyway I can make my viewController.swift behave like it did in the playgroud? Also the function has a "m" instead of "f" if you look at the pictures on the autocomplete.
EDIT:
Thanks to Alblu I realised the stupid error I was making. I was trying to declare the variable straight under the function (as in not inside any other method). When I went to declare this in the viewDidLoad method it worked perfectly. Rookie error.
You're trying to assign the variable to something of the same name, and as a result, Xcode is getting confused between what you mean as a variable and what you mean as a function call. One way of solving this is to have different names for your variables and your functions.