When I write like this
List.<Dictionary.<String, System.Object>>,
the ide tells me
Assets/Scripts/yhj/Model/PrintItem.js(23,71): BCE0044: expecting >, found '>>.
How can I resolve it?
Why would you make a list of dictionaries? Can't you just use the dictionary as the list with the key, value input instead of this? If you wanted to use it like this I would either define an object to be the <String, System.Object> and inserting that as the value and just leave the key as the number.
Or making a list of objects where the object is the <String, System.Object>
#ILiveForVR makes me think about this.thanks.
I solved it, but I'm not sure if the solution is best.
I do it like this:
var list = List.<System.Object>;
for(var i=0;i<list.Count;i++){
var data = list[i] as Dictionary<String, System.Object>;
}
Related
This question already has answers here:
How to get a property by this name in String?
(2 answers)
Closed 4 months ago.
I have a typical class userData.
class userData ({K1:V1,K2:V2,K3:V3})
But i want to retrieve the data from several of them at the same time.
userData1 {K1:V1,K2:V2,K3:V3}
userData2 {K1:V4,K2:V5,K3:V6}
userData3 {K1:V7,K2:V7,K3:V8}
instead of using typical direct way:
print ("${userData1.K1} ${userData2.K1} ${userData3.K1}") ;
I want to access the data in an indirect method by using the key name as a String. Something like this:
String MyKey = 'K1';
print ("${userData1[MyKey]} ${userData2[MyKey]} ${userData3[MyKey]}") ;
Is there a way to retireve the values by using the key name as a String?
Thanks in advance for your help
Your question is unclear.
You may be looking for __getitem__. https://docs.python.org/3/library/operator.html#operator.getitem
Consider
d = dict(a=1, b=2)
When you ask for d['a'], you are calling d.__getitem__('a') behind the scenes.
There is an opportunity for your container class to inherit from dict,
and override __getitem__, if you wish to add behavior like logging,
or something fancier.
This is similar to the str() protocol that you likely are already familiar with.
Overriding __str__ will change the behavior of str(...)
and things that call it, such as format(...).
You should certainly review the getattr documentation.
When you write some code that addresses the need to your liking,
do post it here. https://stackoverflow.com/help/self-answer
Here is one solution that worked for me by creating a new dynamic in my class:
class MyClass{
....
dynamic getProp(String key) => <String, dynamic>{
'K1': K1,
'K2': K2,
'K3': K3,
}[key].toString();
}
}
With this i can retrieve with this line:
print(MyClass.getProp("K1"));
And so to get the data from my array, it work like this:
userData1 {K1:V1,K2:V2,K3:V3}
userData2 {K1:V4,K2:V5,K3:V6}
userData3 {K1:V7,K2:V7,K3:V8}
myKey = "K1";
print ("${userData1.getProp("K1")} ${userData2.getProp("K1")} ${userData3.getProp("K1")}"); // prints "V1 V4 V7"
I have a variable with the following structure:
final Map<String, Map<double, String>> ingredients;
I need to access, as string, the "inner" map keys and values after indexing the external map:
ingredients.keys.elementAt(index)
The above code returns a "regular string", but when accessing the "inner" map:
ingredients.values.elementAt(index).keys
The result prints with round brackets around it. I suppose it occurs because the first example returns a string and the second returns an Itarable. But how do I make it a string without the round brackets?
.toString() does not work.
I can't make it a single Map<String, String> because I need the double value separated from the second string (a unit specification).
I will put the result inside a Flutter Text() widget inside a ListView.builder(), that is the reason of the indexing.
Resuming: I am getting (200)(g) and I need 200g.
Thanks for the attention. Any help is appreciated.
try this -
var _value = ingredients.values.elementAt(index).values;
print(_value.substring(1,_value.length-1));
Not a pretty solution but you can try: ingredients.values.elementAt(index).keys.toString().replaceAll(RegExp(r'[\(\)]'),'');
which will remove the parentheses anywhere in the keys.
I ran into the same problem as you did.
Hope this helps!
I need a list of my map assigned to a List variable outside of this map. How can I do that?
class Lists {
late var list = Map<int, List<int>>();
Lists() {
list[0] = [];
list[1] = [];
list[2] = [];
list[3] = [];
}
}
In another file I then try to assign the list[0] to a List variable:
List<int> listOutside = Lists.list[0];
I then receive this error:
What does the "?" mean and how can I fix that?
Thanks for the help.
Greetings
there are 2 major problems that are there.
You are trying to access a static variable outside a class which is wrong. This problem can be fixed by adding List<int> listOutside = Lists().list[0]; (notice the parentheses)
By accessing the list[0]; you are saying that the element always exists but here the compiler comes into play saying that this is a nullable list (List<int>?) which you are trying to assign to List<int> which is not possible as both have different types. This problem can be quickly fixed by using ! at the end Lists().list[0]!;
But note that this comes with a side effect that if there is no element at 0the index then this will throw NPE.
NOTE: Always avoid using ! in expressions where you are not sure that it is not nullable.
Seems to work when adding a "!".
List<int> listOutside = Lists.list[0]!;
I have no clue why, but anyways it works :D
? means that this field can have the null value. The Dart language now supports sound null safety and to indicate that a variable might have the value null, just add ? to its type declaration:
int? aNullableInt = null;
For your problem, you try to access list variable as a static usage and you use late but you initialize field immediately. For this reason, you can omit late and put static.
This usage could solve your problem:
static var list = Map<int, List<int>>();
List<int> listOutside = Lists.list[0];
I'm trying to iterate over the following data structure: {String: [{String, List<SomethingResponse>}]} where SomethingResponse = {String: dynamic}). I created this model:
class SomethingsResponse {
final Map<String, List<SomethingResponse>> SomethingsResponse;
SomethingsResponse({this.SomethingsResponse});
factory SomethingsResponse.fromJson(data) {
return SomethingsResponse(SomethingsResponse: data.map<String, List<SomethingResponse>>((String key, dynamic value) {
final dataFromCategory = List<SomethingResponse>.from(value.map((x) => SomethingResponse.fromJson(x)));
return MapEntry(key, dataFromCategory);
}));
}
}
When I try getting the keys like this: data.somethingsResponse.toList(), I get an error saying:
Class '_InternalLinkedHashMap<String, List>' has no instance method 'toList'.
I can't iterate over it or really get any kind of data out of it. What am I doing wrong and how can I fix it? I have a feeling the issue is at this line return MapEntry(key, dataFromCategory);, but I tried creating a Map a couple of different ways, and none worked.
If you consult the documentation for Map, you will see that it does not derive from Iterable and therefore cannot be directly iterated over. I presume that this is because it's not obvious what you want to iterate over: keys, values, or key-value pairs?
If you want to iterate over keys, use Map.keys. (In your case: data.somethingsResponse.keys.toList())
If you want to iterate over values, use Map.values.
If you want to iterate over key-value pairs (i.e. MapEntry objects), use Map.entries.
I have data in this order :-
m={ 1:[54,23,98],
9:[8,4,2]
}
I'm trying to achieve something like this using the following code but it's not working. Where am I going wrong? Is there another way of storing and accessing this type of data in dart if my method is completely wrong?
Map<int,List<int>> m;
m[0]=[];
m[0].add(5);
You have to initialise it with empty map first
Map<int, List<int>> m={}; //<- initialise it here
m[0] = [];
m[0].add(5);
print(m); // prints {0: [5]}