I have a list and want it as a string with quotes List myList = [1,2,3]
require O/P as List myList = ["1","2","3"]
i think this works as you need!
void main(List<String> arguments) {
List myList = [1, 2, 3];
List myNewList = [];
myList.forEach((item) {
myNewList.add("\'$item\'");
});
}
so the print of myList would be [1, 2, 3]
and the print of myNewList would be ['1', '2', '3'].
by the way...
The order of the quotation signs is arbitrary and the meaning of
"\'$item\'"
is the same as
'\"$item\"'
The difference is that your output from
"\'$item\'"
would be
['1', '2', '3']
and the output of
'\"$item\"'
would be
["1", "2", "3"].
I think you want something like this:
List<String> myList = <String>["'1'","'2'","'3'"];
Or you can use it this way:
List<String> myList = <String>['\'1'\','\'2'\','\'3\''];
Related
As in Dart you can combine several list items into one according to the following condition:
Given List<String> arr = ['2','3','b','*','4','5','6','-','3','4'];
Get arr = ['23','b','*','456','-','34'];
The list is unknown in advance. It can be of any length and with any sequence of characters. And need to combine only the lines that have numbers in them.
I would be grateful for any suggestions.
You are not describing what should happen if there are multiple special characters or letters. So I have made my example so it will only combine numbers:
void main() {
final arr = ['2', '3', 'b', '*', '4', '5', '6', '-', '3', '4'];
print(combine(arr)); // [23, b, *, 456, -, 34]
}
List<String> combine(List<String> input) {
final output = <String>[];
final buffer = StringBuffer();
for (final string in input) {
if (int.tryParse(string) == null) {
if (buffer.isNotEmpty) {
output.add(buffer.toString());
buffer.clear();
}
output.add(string);
} else {
buffer.write(string);
}
}
if (buffer.isNotEmpty) {
output.add(buffer.toString());
}
return output;
}
You can use ''.join() here:
arr = [''.join(arr[0:2]), arr[2], arr[3], ''.join(arr[4:7]), arr[7], ''.join(arr[8:10])]
If you only want to have a condition where you only join numerical values then you can add a for loop beforehand.
How can I convert 2d list to 1d list with all the elements in dart?
I have this 2d List:
List<List<int>> list2d = [[1, 2], [3, 4]];
What I want is:
List<int> list1d = [1, 2, 3, 4];
By converting the first one (2d) to the second (1d) but without writing any (for/while) loops code, if there any built-in methods like map()/where()/cast()/...etc.
Any ideas?
As other have pointed out, expand does what you want
var list2d = [[1, 2], [3, 4]];
var list1d = list2d.expand((x) => x).toList();
You can also, and perhaps preferably, use a list literal:
var list1d = [for (var list in list2d) ...list];
In general, iterable.expand((x) => e).toList() is equivalent to [for (var x in iterable) ...e].
Simply by using the reduce function like this:
List<int> list1d = list2d.reduce((value, element) {
value.addAll(element);
return value;
});
Definition:
List<T> reduce(List<T> Function(List<T>, List<T>) combine);
You can just use the .expand method:
List<int> list1d = list2d.expand((e) => e).toList();
I have a list1: var list1 = ["a:1", "b:2", "c:3"];
how can I create list2 based on list1 like this: ["a", "b", "c"]
I thought I would have to use split and forEach but I don't know how to combine it
Maybe this works for you, if and only if you always have the same String length
List<String> list1 = ["a:1", "b:2", "c:3"];
List<String> list2 = list1.map((f) => f.substring(0,1)).toList();
Or if you wanted to have ":" as the basis regardless of the String length for each element then you can try the code below
List<String> list1 = ["a:1", "b:2", "c:3"];
List<String> list2 = list1.map((f) => f.split(":")[0]).toList();
Iterate through all items of the list with forEach.
With every item (which is a string), split it using ':' as separator (or, if its always just one character, simply get the first charaxter of the item.
Add the first element of the result of split (or aimply first charaxter of item) to list2.
var list1 = ["a:1", "b:2", "c:3"];
List list2;
list1.asMap().forEach((key, value) {
list2.add(value.replaceAll(':${key + 1}', ''));
});
Here is my code
I have tried to sort the list of objects based on the price of each individual object has. However, I have got this error the expression here has a type of void, and therefore it cannot be used
class Item{
String productName;
double price;
}
List<Item> items = ...;
items.sort((a, b) => a.price.compareTo(b.price));
List.sort modifies the object on which it is call. It doesn't return any value and you have to use the original list.
var list = [3, 1, 2];
list.sort();
print(list); // displays [1, 2, 3]
If you want to inline the .sort() to use the list directly, you can use the cascade notation:
var list = [3, 1, 2]..sort();
print(list); // displays [1, 2, 3]
// or
var list = [3, 1, 2];
print(list..sort()); // displays [1, 2, 3]
I have this map:
var temp= {
'A' : 3,
'B' : 1,
'C' : 2
};
How to sort the values of the map (descending). I know, I can use temp.values.toList()..sort().
But I want to sort in context of the keys like this:
var temp= {
'B' : 1,
'C' : 2
'A' : 3,
};
This example uses a custom compare function which makes sort() sort the keys by value. Then the keys and values are inserted into a LinkedHashMap because this kind of map guarantees to preserve the order.
Basically the same as https://stackoverflow.com/a/29629447/217408 but customized to your use case.
import 'dart:collection';
void main() {
var temp= {
'A' : 3,
'B' : 1,
'C' : 2
};
var sortedKeys = temp.keys.toList(growable:false)
..sort((k1, k2) => temp[k1].compareTo(temp[k2]));
LinkedHashMap sortedMap = new LinkedHashMap
.fromIterable(sortedKeys, key: (k) => k, value: (k) => temp[k]);
print(sortedMap);
}
Try it on DartPad
The SplayTreeMap has a named constructor which accepts map and a comparator which is used to sort given map while building new map. Since SplayTreeMap is a descendant of Map you can easily substitute it.
import 'dart:collection';
void main() {
var unsorted = {'A': 3, 'B': 1, 'C': 2};
final sorted = SplayTreeMap.from(
unsorted, (key1, key2) => unsorted[key1].compareTo(unsorted[key2]));
print(sorted);
}
final Map<String, ClassCategory> category;
...
Map<String, ClassCategory> sorted = SplayTreeMap.from(category,
(key1, key2) => category[key1]!.title.compareTo(category[key2]!.title));
for (var item in sorted.entries) {
...
}