Inbuild List Methods In Dart
In Dart programming, the List data type is like exhibits in other programming dialects. A list is utilized to address an assortment of items. It is an arranged gathering of objects. The core libraries in Dart are answerable for the presence of the List class, its creation, and manipulation.
This blog will explore the Inbuild List Methods In Dart. We will see a few of the most common ones you should know for your next app. We are going to learn how to use it in our next projects.
for():
Runs a function on every component in the list
void main() {
List<String> bikes = [
'Harley-Davidson',
'Royal Enfield Hunter',
'Yamaha R15 V4',
'Yezdi Roadster',
'KTM Duke 200 ',
'Bajaj Pulsar NS200',
];
for (var bike in bikes) {
print(bike);
}
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
map():
Produces another list in the wake of changing every component in a given list
void main() {
List<String> bikes = [
'Harley-Davidson',
'Royal Enfield Hunter',
'Yamaha R15 V4',
'Yezdi Roadster',
'KTM Duke 200 ',
'Bajaj Pulsar NS200',
];
List<String> myBikes = bikes.map((bike) => 'I want $bike').toList();
for (var bike in myBikes) {
print(bike);
}
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
contains():
Checks to affirm that the given component is in the list
void main() {
List<String> bikes = [
'Harley-Davidson',
'Royal Enfield Hunter',
'Yamaha R15 V4',
'Yezdi Roadster',
'KTM Duke 200 ',
'Bajaj Pulsar NS200',
];
String key = 'Harley-Davidson';
String bikekey = 'myBike';
print('bikes contains $key');
print(bikes.contains(key));
print('bikes contains $bikekey');
print(bikes.contains(bikekey));
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
sort():
Order the components in view of the gave ordering function
void main() {
List<String> bikes = [
'Harley-Davidson',
'Royal Enfield Hunter',
'Yamaha R15 V4',
'Yezdi Roadster',
'KTM Duke 200 ',
'Bajaj Pulsar NS200',
];
print('Before: $bikes');
bikes.sort((a, b) => a.compareTo(b));
print('After: $bikes');
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
reduce() & fold():
Compresses the components to a solitary value and utilizing the given function
void main() {
List<int> marks = [70, 50, 95];
int totalMarks = marks.reduce((curr, next) => curr + next);
print('totalMarks : $totalMarks');
int initialGraceMarks = 40;
int graceTotalMarks = marks.fold(initialGraceMarks, (curr, next) => curr + next);
print('graceTotalMarks : $graceTotalMarks');
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
every():
Affirms that each component fulfills the test
void main() {
List<Map> attendance = [
{
'name': 'Yash',
'totalAttendance': 75,
},
{
'name': 'Rakhi',
'totalAttendance': 68,
},
{
'name': 'Pragati',
'totalAttendance': 38,
},
];
int levelOne = 45;
bool passedOne = attendance.every((totalAttendance) => totalAttendance['totalAttendance'] > levelOne);
print('levelOne :$levelOne PASSED : $passedOne');
int levelTwo = 33;
bool passedTwo = attendance.every((totalAttendance) => totalAttendance['totalAttendance'] > levelTwo);
print('levelTwo :$levelTwo PASSED : $passedTwo');
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
where(), firstWhere() & singleWhere():
Returns a collection of components that fulfill a test. firstWhere() returns the first match in the list, while singleWhere() returns the first match given there is precisely one match.
void main() {
List<Map> attendance = [
{
'name': 'Yash',
'totalAttendance': 75,
},
{
'name': 'Rakhi',
'totalAttendance': 68,
},
{
'name': 'Pragati',
'totalAttendance': 38,
},
];
int levelOne = 45;
List<Map> levelOneResult = attendance.where(
(totalAttendance) => totalAttendance['totalAttendance'] > levelOne,
).toList();print('levelOne :$levelOne levelOneResult : $levelOneResult');Map result1 = attendance.firstWhere(
(totalAttendance) => totalAttendance['totalAttendance'] == 38,
);print('Result: $result1');Map result2 = attendance.singleWhere(
(totalAttendance) => totalAttendance['totalAttendance'] < 38,
orElse: () => null,
);print('Result: $result2');
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
List.from():
Makes another list from the given assortment
class StudentAttendance {
String name;
int totalAttendance;
StudentAttendance(this.name, this.totalAttendance);
factory StudentAttendance.fromMap(Map data) => StudentAttendance(
data['name'],
data['totalAttendance'],
);
@override
toString() {
return '$name : $totalAttendance';
}
}
void main() {
List<Map> attendance = [
{
'name': 'Yash',
'totalAttendance': 75,
},
{
'name': 'Rakhi',
'totalAttendance': 68,
},
{
'name': 'Pragati',
'totalAttendance': 38,
},
];
List<StudentAttendance> attendanceList = List.from(attendance.map((data) => StudentAttendance.fromMap(data)));
for (var data in attendanceList) {
print(data.toString());
}
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
Conclusion:
In the article, I have explained the Inbuild List Methods In Dart basic structure in a flutter; you can modify this code according to your choice. This was a small introduction to Input Chip User Interaction from my side, and it’s working using Flutter.
I hope this blog will provide you with sufficient information on Trying the Inbuild List Methods in your projects. We will show you the most common Inbuild List Methods in your applications. So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter-related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.