Don't create a lambda when tear-off will do - dart

I have a get function in my dart file where i have used a lambda. I am using DartLint which tells me not to create lambda if tear-off will do. I am not sure how to use a tear-off in my use case.
///To change data to our Stream using Sink.
Function(dynamic) get changeData => (event) {
_dataBlocController.add(event);
};
This is my lambda function, how will a tear-off look for this. I have gone through the documentation, but i am not getting a syntactically correct solution.
Thank you for the help!

This means that the closure is useless.
Instead of:
get changeData => (event) => _foo.add(event);
you can do:
get changeData => _foo.add;

Related

Cannot get class name from node after locating it in playwright

I have an SVG object like that:
<svg class="class-a color-green marker" .../>
In Playwright I want to get an exact list of classes of this element. I use the following code to locate it:
page.locator(".status-marker").first
The node is located properly, but when I call evaluate("node => node.className") on it, I get an empty dict, like the locator stripped all information about classes at all.
In general, it doesn't matter how I get the handle of this element, I always get an empty dict on evaluate("node => node.className").
Calling page.locator(".status-marker").first.is_visible() returns True, so the object exists.
Also if I run page.locator(".status-marker").first.evaluate("node => node.outerHTML") I'll get the full HTML of that node, that does have the class name included. I could parse that, but it would be pretty clunky solution.
I found out that I could use expect(locator).to_have_class(), but If the node has more than one class I need to put all of them for it to pass, when I care only on one of them (the other classes are dynamically generated, so I can't even know about them during tests).
Edit:
Here's some additional sample:
assert page.locator(".marker").first.evaluate("node => node.className") == {}
expect(page.locator(".marker").first).to_have_class("text-green-1")
The first assert passes - the evaluate("node => node.className") returns an empty dict. The expect() fails with the following error:
AssertionError: Locator expected to have class 'text-green-1'
E Actual value: inline pr-2 text-green-1 marker svelte-fa s-z-WEjw8Gh1FG
I've found a way to reproduce it (it happens to me in font awesome plugin for svelte):
def test_svelte_fa(page):
page.goto("https://cweili.github.io/svelte-fa/")
item = page.locator(".svelte-fa").first
assert item.is_visible()
assert "svelte-fa" in item.evaluate("node => node.className")
In your example, the className of an SVG is an SVGAnimatedString object. Which is not serializable.
If you do JSON.stringify($('.svelte-fa').className) on the browser, you will see that the value is {}.
Values returned by the evaluate function needs to be serializable.

How to fix this error in the following codes?

Could someone tell me how to fix this please?
This is in the main.dart error line.
https://i.stack.imgur.com/tFlDp.png
Another file's code, I think this might related to the error.
List<CovidTodayResult> covidTodayResultFromJson(String str) => List<CovidTodayResult>.from(json.decode(str).map((x) => CovidTodayResult.fromJson(x)));
String covidTodayResultToJson(List<CovidTodayResult> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson()))); ```
As your image states, you're declaring the return type as Future<CovidTodayResult>, but you're not returning a Future. If you want this specific code to work you should change your return type to List<CovidTodayResult> or if you're using await in your method, which I can't see, Future<List<CovidTodayResult>>.

Google Dart : How does .where() function work?

var fruits = ['apples', 'oranges', 'bananas'];
fruits[0]; // apples
fruits.add('pears');
fruits.length == 4;
fruits.where((f) => f.startsWith('a')).toList();
The example in the documentation shows the above.
I dont really understand the documentation of the method either.
https://api.dartlang.org/stable/1.21.1/dart-collection/IterableMixin/where.html
I currently see a lambda function as a parameter inside where, with where having the argument f. What is f though? Im a bit confused.
It would be great if I could see a working example. As it stands now I dont really get it. I dont know how it works or what it really does apart from that it acts as some sort of filter.
Is an anonymous function and f is the parameter it accepts
(f) => f.startsWith('a')
where(...) calls that passed function for each element in fruits and returns an iterable that only emits the values where the function returned true
where(...) is lazy, therefore the iteration and call of the passed function will only happen when the result is actually accessed, like with .toList().
DartPad example
update
"anonymous" means the function has no name in contrary to a named function like
myFilter(f) => f.startsWith('a');
main() {
fruits.where(myFilter).toList();
}
also
myFilter(f) => f.startsWith('a');
is just a shorter form of
myFilter(f) {
return f.startsWith('a');
}

firefox addon webrequest.addListener misbehaving

I want to examine http requests in an extension for firefox. To begin figuring out how to do what I want to do I figured I'd just log everything and see what comes up:
webRequest.onResponseStarted.addListener(
(stuff) => {console.log(stuff);},
{urls: [/^.*$/]}
);
The domain is insignificant, and I know the regex works, verified in the console. When running this code I get no logging. When I take out the filter parameter I get every request:
webRequest.onResponseStarted.addListener(
(stuff) => {console.log(stuff);}
);
Cool, I'm probably doing something wrong, but I can't see what.
Another approach is to manually filter on my own:
var webRequest = Components.utils.import("resource://gre/modules/WebRequest.jsm", {});
var makeRequest = function(type) {
webRequest[type].addListener(
(stuff) => {
console.log(!stuff.url.match(/google.com.*/));
if(!stuff.url.match(/google.com.*/))
return;
console.log(type);
console.log(stuff);
}
);
}
makeRequest("onBeforeRequest");
makeRequest("onBeforeSentHeaders");
makeRequest("onSendHeaders");
makeRequest("onHeadersReceived");
makeRequest("onResponseStarted");
makeRequest("onCompleted");
With the console.log above the if, I can see the regex returning true when I want it to and the code making it past the if. When I remove the console.log above the if the if no longer gets executed.
My question is then, how do I get the filtering parameter to work or if that is indeed broken, how can I get the code past the if to be executed? Obviously, this is a fire hose, and to begin searching for a solution I will need to reduce the data.
Thanks
urls must be a string or an array of match patterns. Regular expressions are not supported.
WebRequest.jsm uses resource://gre/modules/MatchPattern.jsm. Someone might get confused with the util/match-pattern add-on sdk api, which does support regular expressions.

How to create a Dart Stream from a JavaScript listener

I would like to create a Stream which should contain the same elements like the callback in the following code:
chromeTabs['onUpdated'].callMethod('addListener',
[(tabId, changeInfo, tab) => print("tabId = $tabId")]);
I read the tutorials/articles from Chris Buckett and am not sure if it is possible to create a Stream elements at the moments when the first Consumer comes. In the code above, it would mean to get the javascript listener registered when the Dart Stream get listened.
The following code cannot work due to referencing to updateStreamController before it is being initialised:
var updateStreamController = new StreamController<int>(onListen: () =>
chromeTabs['onUpdated'].callMethod('addListener', [(tabId, changeInfo, tab) =>
updateStreamController.add(tabId)]);`
Unfortunately, the onListen property is only settable via the constructor.
Thanks in advance for your help
You can simply declare the variable before to initialize it :
var updateStreamController;
updateStreamController = new StreamController<int>(onListen: () =>
chromeTabs['onUpdated'].callMethod('addListener',
[(tabId, changeInfo, tab) => updateStreamController.add(tabId)]));

Resources