Convert from Set to List in Dart / Flutter? - dart

I have a Set of type Set<WordPair> & I want to convert it to List of type List<String> because I want to save it using SharedPreferences API in Flutter
Currently, my state looks like
final Set<WordPair> _saved = Set<WordPair>();
Now I want to save the _saved variable into SharedPreferences
If I do, prefs.setStringList('saved', _saved.toList()); it gives me compilation error like
[dart] The argument type 'Set' can't be assigned to the parameter type 'List'. [argument_type_not_assignable]
How do I store complete _saved variable in SharedPreferences?

The reason you are unable to save it in the Preferences even after converting it into a list is that you are trying to save StringList (prefs.setStringList) whereas your set is of type WordPair
A workaround for that could be,
prefs.setStringList('saved', _saved.map((WordPair wordPairItem) => wordPairItem.toString()).toList());
i.e converting each item into String and then save it as StringList
To your follow-up question in the comments:
how do I convert from List<String> to Set<WordPair> in initState? As there is no .toWordPair() & wrapping up takes 2 args so Idk how to do it?
As per documentation, WordPair is Representation of a combination of 2 words, first and second. Therefore, you can break the String into two substrings and pass those 2 substrings in the respective argument positions.
[Update] (solution proposed by the Author of the follow-up question)
The solution can be found at here

Related

How to access map keys through index? Dart

dartques = {'Color':[], 'Fruits':[], 'Hobbies':[]};
How to access the values using index in map?
I need to access only key or value using index.
Just like we do in list
=>list[1]
you can get it like this
var element = dartques.values.elementAt(0);
also for Dart 2.7+ you can write extension function
extension GetByKeyIndex on Map {
elementAt(int index) => this.values.elementAt(index);
}
var element = dartques.elementAt(1);
For accessing the values by index, there are two approaches:
Get Key by index and value using the key:
final key = dartques.keys.elementAt(index);
final value = dartques[key];
Get value by index:
final value = dartques.values.elementAt(index);
You may choose the approach based on how and what data are stored on the map.
For example if your map consists of data in which there are duplicate values, I would recommend using the first approach as it allows you to get key at the index first and then the value so you can know if the value is the wanted one or not.
But if you do not care about duplication and only want to find a value based on Index then you should use the second approach, as it gives you the value directly.
Note: As per this Answer By Genchi Genbutsu, you can also use Extension methods for your convenience.
Important:
Since the default implementation for Map in dart is LinkedHashmap you are in great luck. Otherwise Generic Map is known for not maintaining order in many programming languages.
If this was asked for any other language for which the default was HashMap, it might have been impossible to answer.
You can convert it to two lists using keys and values methods:
var ques = {'Color':['a'], 'Fruits':['b'], 'Hobbies':['c']};
List keys = ques.keys.toList();
List values = ques.values.toList();
print (keys);
print (values);
The output:
[Color, Fruits, Hobbies]
[[a], [b], [c]]
So you can access it normally by using keys[0], for example.

ExtJs6: Ext.ComponentQuery.query('#autogenerated_id') returns an empty array if the component has an itemId

I have some ExtJs component.
I set itemId for it, but id is autogenerated.
Now Ext.getCmp('autogenerated-id') returns my component.
But Ext.ComponentQuery.query('#autogenerated-id') returns an empty array.
But:
Ext.ComponentQuery.query('[id=assets-information-form-1918]') again returns my component. :)
I use ExtJs 6.5.3 classic.
It seems like itemId config property hides autogenerated id from Ext.ComponentQuery, so they become mutually exclusive.
I don't need other means for search or advice to set id for the component, to write letter to Sencha support or to write post on their forum.
I need:
Means to force my Ext.ComponentQuery.query('#autogenerated-id') to
find the Component for which getId() returns 'autogenerated-id'.
If it is not possible by design, I need a link to some documentation
describing this behavior, a link to some bug report, or a filename and line number in ExtJs sources + a little snippet copy/paste from there.
From the documentation
Summary Provides searching of Components within Ext.ComponentManager
(globally) or a specific Ext.container.Container on the document with
a similar syntax to a CSS selector. Returns Array of matching
Components, or empty Array.
Ext.ComonpentQuery.query('#itemId') returns and array. Your cold above is using the auto-generated id of the component. The # indicates to query based on the component itemId and not the component id.
Try
Ext.ComponentQuery.query('assets-information-form-1918');
which will return an array, as noted in the documentation.
Ext.getCmp()
This is shorthand reference to Ext.ComponentManager#get. Looks up an
existing Ext.Component by id
Therefore it returns the component object.
Ext.ComponentQuery.query('#itemId')[0] would return the first object in the array.
Ext.ComponentQuery is the Sencha preferred method because it is more powerful when used as it does return an array so you an also query items by xtype and other attributes.

How to update value in angular ui typeahead directive if no matching option is found

I've an array of objects containing title and salary which is used in typeahead directive.
I display department name and get entire object as value.
If none of the options match, I want user entered string to be converted to object still. Is there any way to update that?
This answer is pretty late, but I would just use ng-blur (to trap the end of the users input) along with a variable bound to typeahead-no-results. Then test if the variable is true in the method bound to ng-blur, and, if so, make an object out of the String supplied by the user and push it to your data source. Simple example found here.

Any reason for URLEncodedUtils.parse return List< NameValuePair >

https://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html#parse(java.net.URI,%20java.lang.String)
why not return Map< String, List< String > >
if it returns Map, user can easily query by key to get whatever they want.
otherwise user should iterator the whole list to find what they want.
Thanks
Because this is a list. If you read carefully the explanation, says:
Returns a list of NameValuePairs as built from the URI's query portion. For example, a URI of http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three NameValuePairs, one for a=1, one for b=2, and one for c=3.
That means that you cannot use a list to acces by value, because these type of data structures aren't designed for that. They doesn't have any "key" to use.
In this website, Jon Skeet wrote a very clear answer about you cannot use a shortcut to acces a List<NameValuePair> value using a name or a key or for similar structures.
Here you can check the post: Get ArrayList<NameValuePair> value by name
Inside of the same post, you'll see different options to solve your problem. (Or could guide to you to find the solution)

GWT JSONObject adding an additional incorrect key when converting overlay type to json string

I'm encountering the following problem - I have simple GWT overlay types, and I'm trying to convert one to a JSON string on the client; I'm simply doing:
new JSONObject(this).toString();
The conversion works, but it adds an additional, incorrect key to the json string, such as:
{"key1":"value1", "key2":value2, "$H":1}
where "$H":1 doesn't correspond to anything in my overlay type.
Any idea why this is?
Any help is appreciated on this, thanks.
This issue is define in this link
The $H property comes from the
implementation of
JavaScriptObject#hashCode() (in
com.google.gwt.cire.client.impl.Impl#getHashCode(Object)).
In your case, this is due to
AbstractEditableCell maintaining a map
of value keys to their "view data",
and your use (I guess) of the default
ProvidesKey implementation
(SimpleProvidesKey) which directly
returns the item.
So, when rendering, the EditTextCell
calls getViewData, which looks up the
key in the map (and thus needs the
hashcode of the key, hence the call to
hashCode), and the key is your JSO
(hence the new $H property).
I believe that giving a ProvidesKey
implementation (in you case, returning
the name property for instance) to the
Celltable would solve your issue.

Resources