List<String> countries = ["Canada", "Brazil", "USA", "Japan", "China","UK", "Uganda", "Uruguay"];
List<String> filter = [];
filter.addAll(countries);
filter.retainWhere((countryone){
return countryone.toLowerCase().contains("U".toLowerCase());
//you can add another filter conditions too
});
print(filter); //list of countries which contains 'U' on name
//output: [USA, UK, Uganda, Uruguay]
Full Dart/Flutter code Example:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home()
);
}
}
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
List<String> countries = ["Canada", "Brazil", "USA", "Japan", "China","UK", "Uganda", "Uruguay"];
List<String> filter = [];
filter.addAll(countries);
filter.retainWhere((countryone){
return countryone.toLowerCase().contains("U".toLowerCase());
//you can add another filter conditions too
});
print(filter); //list of countries which contains 'U' on name
//output: [USA, UK, Uganda, Uruguay]
return Scaffold(
appBar: AppBar(
title: Text("Filter Array in Dart"),
backgroundColor: Colors.redAccent
),
body: Container(
margin: EdgeInsets.all(10),
alignment: Alignment.topCenter,
child: Column(
children: filter.map((countryone){
return Card(
child:ListTile(
title: Text(countryone)
)
);
}).toList(),
),
)
);
}
}