I made three lists, the first for free servers, the second for paid servers, and the third list includes both together.
The problem is that when he adds the lists in the third list, he adds them in order
In other words, he adds the whole first list above and the second list below
I want a way to combine the two lists and arrange them according to name, number, or any other thing. The important thing is that they are not arranged according to the same list (the first is above and the second is below)
You can use sort method on 'allservs'. Here is a working sample that sorts combined list based on 'cnumber':
var servers = List.from([
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 7,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 2,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 1,
'isvip': false,
},
]);
var newServers = List.from([
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 7,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 12,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 3,
'isvip': false,
},
]);
get allServers => (List.from(newServers) + List.from(servers))
..sort((a, b) => (a['cnumber'] >= b['cnumber']) ? 1 : -1);
void main() {
print(allServers);
}
The simples way would be to do it like this:
var allServers = [...servers, ...newsServers ];
For Example, you can see this sample in DartPad:
image
which would result in outputing:
[{name: Singapore}, {name1: Singapore1}]
Related
Here I have a list of maps.
[
{
'name': 'name1',
'age': 30,
},
{
'name': 'name2',
'age': 20,
},
{
'name': 'name1',
'age': 15,
},
]
I need to access to 'name': 'name1' map from above list.
-- Access or extract maps --
i. e. List[0] and(or) List[2]
How do I do this kind of thing???
You can loop through the list and find items, .where will return list of found item. .firstWhere for single item return.
final data = [
{
'name': 'name1',
'age': 30,
},
{
'name': 'name2',
'age': 20,
},
{
'name': 'name1',
'age': 15,
},
];
final findValue = "name1";
final result = data.where((element) => element["name"] == findValue);
print(result); //({name: name1, age: 30}, {name: name1, age: 15})
result.forEach((element) {
print("${element["name"]} ${element["age"]}");
});
More about List
I need to create 96 Map objects in a List with the following key-value pairs
{
'id': 1,
'time': '00:00-00:15',
'slotNumber': '01',
'clicked': false
}
Although this is something that is easily achievable using loops, the main complication starts when it comes to generating the time range. The time key in every object needs to be at an interval of 15 minutes each and should be in a 24-hour format. For example, the next few time ranges need to be 00:15-00:30, 00:30-00:45, 00:45-01:00, and so on. I tried hard to look for a package that would answer all my prayers but couldn't find any.
The final output needs to look something like this:
var kSlots = [
{'id': 1, 'time': '00:00-00:15', 'slotNumber': '01', 'clicked': false},
{'id': 2, 'time': '00:15-00:30', 'slotNumber': '02', 'clicked': false},
{'id': 3, 'time': '00:45-01:00', 'slotNumber': '03', 'clicked': false},
{'id': 4, 'time': '01:00-01:15', 'slotNumber': '04', 'clicked': false},
{'id': 5, 'time': '01:15-01:30', 'slotNumber': '05', 'clicked': false},
{'id': 6, 'time': '01:30-01:45', 'slotNumber': '06', 'clicked': false},
{'id': 7, 'time': '01:45-02:00', 'slotNumber': '07', 'clicked': false},
{'id': 8, 'time': '02:00-02:15', 'slotNumber': '08', 'clicked': false}]
As I now need to generate the time from a given start date which is in String, I tried modifying #jamesdlin 's answer a bit to achieve that. But it throws the following error:
Uncaught Error: FormatException: Invalid date format
08:00
String getTimeRange(int i) {
var midnight = DateTime.parse(DateFormat('HH:mm').format(DateTime.parse('08:00')));
const interval = Duration(minutes: 15);
var start = midnight.add(interval * i);
var end = start.add(interval);
var formatTime = DateFormat('HH:mm').format;
return '${formatTime(start)}-${formatTime(end)}';
}
void main() {
var slots = [
for (var i = 0; i < 20; i += 1)
<String, dynamic>{
'id': i + 1,
'time': getTimeRange(i),
'slotNumber': '${i + 1}'.padLeft(2, '0'),
'clicked': false,
},
];
slots.forEach(print);
}
Loop over an index and use the index as a multiplier for a Duration of 15 minutes.
You then either can add that multiplied Duration to a DateTime representing midnight (if your time ranges represent points in time) or format those Durations directly (if they represent durations).
I assuming that your times represent points in time:
import 'package:intl/intl.dart';
String getTimeRange(int i) {
var midnight = DateTime.utc(2022, 1, 1);
const interval = Duration(minutes: 15);
var start = midnight.add(interval * i);
var end = start.add(interval);
var formatTime = DateFormat('HH:mm').format;
return '${formatTime(start)}-${formatTime(end)}';
}
void main() {
var slots = [
for (var i = 0; i < 8; i += 1)
<String, dynamic>{
'id': i + 1,
'time': getTimeRange(i),
'slotNumber': '${i + 1}'.padLeft(2, '0'),
'clicked': false,
},
];
slots.forEach(print);
}
You can set use the DateTime class instead of String and manipulate it as you desire: https://api.flutter.dev/flutter/dart-core/DateTime-class.html
I have two lists, 1 is a list of Map items, and another list which is the order.
I would like to sort the items based on their description attribute and compare them with the order list and have them inserted at the top.
import 'package:collection/collection.dart';
void main() {
List<String> order = [
'top european',
'top usa',
'top rest of the world'
];
List<Map> items = [
{'id': 0, 'id2': 5, 'description': 'Top USA'},
{'id': 2, 'id2': 2, 'description': 'Top A'},
{'id': 3, 'id2': 0, 'description': 'Top Z'},
{'id': 6, 'id2': 6, 'description': 'Top Rest of the world'},
{'id': 4, 'id2': 4, 'description': 'Top C'},
{'id': 5, 'id2': 1, 'description': 'Top D'},
{'id': 1, 'id2': 3, 'description': 'Top European'},
];
//this works but adds the items at the end
items.sort((a,b) {
return order.indexOf(a['description'].toLowerCase()) -
order.indexOf(b['description'].toLowerCase());
});
///Results: print(items);
// List<Map> items = [
// {'id': 2, 'id2': 2, 'description': 'Top A'},
// {'id': 3, 'id2': 0, 'description': 'Top Z'},
// {'id': 4, 'id2': 4, 'description': 'Top C'},
// {'id': 5, 'id2': 1, 'description': 'Top D'},
// {'id': 1, 'id2': 3, 'description': 'Top European'},
// {'id': 0, 'id2': 5, 'description': 'Top USA'},
// {'id': 6, 'id2': 6, 'description': 'Top Rest of the world'},
// ];
}
SOLUTION: I also tried this approach which is not ideal, but it works.
List <Map> itemsOrder = items
.where(
(ele) => order.contains(ele['description'].toString().toLowerCase()))
.toList();
itemsOrder.sort((a, b) {
return order.indexOf(a['description'].toLowerCase()) -
order.indexOf(b['description'].toLowerCase());
});
items.removeWhere(
(ele) => order.contains(ele['description'].toString().toLowerCase()));
itemsOrder = itemsOrder.reversed.toList();
for (int i = 0; i < itemsOrder.length; i++) {
items.insert(0, itemsOrder[i]);
}
///Results: print(items);
// List<Map> items = [
// {'id': 1, 'id2': 3, 'description': 'Top European'},
// {'id': 0, 'id2': 5, 'description': 'Top USA'},
// {'id': 6, 'id2': 6, 'description': 'Top Rest of the world'},
// {'id': 2, 'id2': 2, 'description': 'Top A'},
// {'id': 3, 'id2': 0, 'description': 'Top Z'},
// {'id': 4, 'id2': 4, 'description': 'Top C'},
// {'id': 5, 'id2': 1, 'description': 'Top D'},
// ];
Ideally, I would like to use sortBy or sortByCompare but unfortunately, I cannot find a proper example or get a grasp of how to use it.
The way I would fix this is to find the index of the description in the order list and if it cannot be found, I would use a number that is out of index inside the order list to indicate that this item should be at the bottom of the list.
This would be my solution:
void testIt() {
final outOfBounds = order.length + 1;
const description = 'description';
items.sort(
(lhs, rhs) {
final lhsDesc = (lhs[description] as String).toLowerCase();
final rhsDesc = (rhs[description] as String).toLowerCase();
final lhsIndex =
order.contains(lhsDesc) ? order.indexOf(lhsDesc) : outOfBounds;
final rhsIndex =
order.contains(rhsDesc) ? order.indexOf(rhsDesc) : outOfBounds;
return lhsIndex.compareTo(rhsIndex);
},
);
}
And the result is:
[{id: 1, id2: 3, description: Top European}, {id: 0, id2: 5, description: Top USA}, {id: 6, id2: 6, description: Top Rest of the world}, {id: 2, id2: 2, description: Top A}, {id: 3, id2: 0, description: Top Z}, {id: 4, id2: 4, description: Top C}, {id: 5, id2: 1, description: Top D}]
I have a List<Map<String, String>> like below
[
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
]
And, the ID list **List** as ['bb', 'aa']. By using the ID list, I want to return a new list ['Jane', 'John'] as **List _selectedList**.
I have tried to do it with the .**indexWhere**, however, I am stuck on the List where it has more than one value.
How can I return the List only with the name-value when there is more than one value to look for?
void main() {
var a = [
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
];
var b = ['bb', 'aa'];
var c = a.where((m) => b.contains(m['id'])).map((m) => m['name']);
print(c);
}
Result
(John, Jane)
Use a set to filter out the IDs efficiently.
var ids = ["aa", "cc"];
var idSet = Set<String>.from(ids);
var json = [
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
];
var _selectedList = json.where((data) => idSet.contains(data["id"]))
.map((data) => data["name"]).toList();
print(_selectedList);
Here, .where filters out the data where the ID matches one in the input list "IDs". Sets make the process efficient. Then, the resultant objects are passed to .map where the "name" field is extracted. Finally, the whole thing is converted into a list thus returning the list of names.
Output of the above code:
[John, Lisa]
This question already has answers here:
Sort a list of objects in Flutter (Dart) by property value
(12 answers)
Closed 3 years ago.
I need to sort a list in Flutter, that's an object List, I have a model and that model has a property named as isFeatured, I need that all the elements with isFeatured as true be in the first positions of the List.
I mean I could have something like:
[
{
id: 1,
name: 'Test',
isFeatured: false,
},
{
id: 2,
name: 'Test 3',
isFeatured: true,
},
{
id: 3,
name: 'Test 5',
isFeatured: false,
},
{
id: 4,
name: 'Test 34',
isFeatured: true,
}
]
And the elements with isFeatured as true should be in the first position.
you can use sort method with custom comparator for bool
myList.sort((a, b) => (a.isFeature ? 1 : 0) - (b.isFeature ? 1 : 0));
Use List.sort
var myList = [
{
'id': 1,
'name': 'Test',
'isFeatured': false,
},
{
'id': 2,
'name': 'Test 3',
'isFeatured': true,
},
{
'id': 3,
'name': 'Test 5',
'isFeatured': false,
},
{
'id': 4,
'name': 'Test 34',
'isFeatured': true,
}
];
myList.sort((a, b) => (b['isFeatured']?1:0).compareTo(a['isFeatured']?1:0));