Is Map of lists not possible in dart? - flutter

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]}

Related

Dart - Read Keys and Values from a Map inside a Map

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!

Flutter GetX RxList assign issue

Im trying to convert old code to new code syntax. I have a issue with RxList.
So I change postModel.assign(postDetail);
But In my news_detail page How I can access to value?
First of all you shouldn''t use postModel as a List as your API clearly returns a single post (NewsModel) by id and not a list of post (List of NewsModel). So using var postModel = <NewsModel>[].obs; is totally unnecessary in my opinion.
What you could do is:
final postModel = NewsModel().obs;
And then on API call:
postModel.value = postDetail;
And then on View:
Image.network(controller.postModel.value.imageUrl);
postModel is a List.
So you would need to access an item in that list, using an int index.
Something like this:
return Image.network(newsDetailController.postModel[0].imageUrl);

How can I assign a List<int> value of a map to a list outside of that map?

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];

Iterate over _InternalLinkedHashMap not working

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.

unity,how to use generic by using js?

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>;
}