[英]Flutter filter list
這就是當我嘗試在不過濾的情況下獲取列表時發生的情況,它運行良好。 但是當我嘗試使用條件不起作用的條件來過濾列表時,這就是當我嘗試在不過濾的情況下獲取列表時發生的情況,它工作正常。 但是當我嘗試使用條件不起作用的條件來過濾列表時,這就是當我嘗試在不過濾的情況下獲取列表時發生的情況,它工作正常。 但是當我嘗試使用 where 條件不起作用時過濾列表
late String _email = '';
var _users = [];
String api =
'https://raw.githubusercontent.com/ilkacase1234/Amore/main/Users.json';
login() async {
try {
final response = await http.get(Uri.parse(api));
var data = json.decode(response.body) as List;
setState(() {
_users = data;
});
} catch (e) {
EasyLoading.showError(e.toString());
}
}
@override
void initState() {
login();
super.initState();
}
check() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final String? login = prefs.getString('login');
if (login != null) {
Navigator.pushReplacementNamed(context, HomePage.id);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
elevation: 0,
bottom: PreferredSize(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
children: [
TextField(
onChanged: (val) {
setState(() {
_email = val;
});
},
keyboardType: TextInputType.url,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5)),
fillColor: Colors.white,
contentPadding: EdgeInsets.zero,
hintText: 'Halkan ku qor magalada gurigu ku yaalo',
prefixIcon: IconButton(
icon: Icon(Icons.menu),
onPressed: () {},
),
suffixIcon: IconButton(
icon: Icon(Icons.search),
onPressed: () {},
),
filled: true,
),
),
],
),
),
preferredSize: Size.fromHeight(56),
),
),
body: ListView.builder(
itemCount: _users.length,
itemBuilder: (context, index) {
final currentUser = _users[index];
if (_users.isEmpty) {
return Center(child: Text('No data'));
} else {
return Card(
child: ListTile(
leading: CircleAvatar(
radius: 28,
backgroundImage:
NetworkImage(currentUser['image'].toString()),
),
title: Text(currentUser['email'].toString()),
subtitle: Column(
children: [
Text(currentUser['password'].toString()),
Text(currentUser['name'].toString()),
],
),
),
);
}
}),
);
}
}
late String _email = '';
var _users = [];
String api =
'https://raw.githubusercontent.com/ilkacase1234/Amore/main/Users.json';
login() async {
try {
final response = await http.get(Uri.parse(api));
var data = json.decode(response.body) as List;
var data1 = data
.where((el) => el['email']
.toLowerCase()
.contains(_email.toLowerCase()))
.toList();
setState(() {
_users = data1;
});
} catch (e) {
EasyLoading.showError(e.toString());
}
}
@override
void initState() {
login();
super.initState();
}
check() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final String? login = prefs.getString('login');
if (login != null) {
Navigator.pushReplacementNamed(context, HomePage.id);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
elevation: 0,
bottom: PreferredSize(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
children: [
TextField(
onChanged: (val) {
setState(() {
_email = val;
});
},
keyboardType: TextInputType.url,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5)),
fillColor: Colors.white,
contentPadding: EdgeInsets.zero,
hintText: 'Halkan ku qor magalada gurigu ku yaalo',
prefixIcon: IconButton(
icon: Icon(Icons.menu),
onPressed: () {},
),
suffixIcon: IconButton(
icon: Icon(Icons.search),
onPressed: () {},
),
filled: true,
),
),
],
),
),
preferredSize: Size.fromHeight(56),
),
),
body: ListView.builder(
itemCount: _users.length,
itemBuilder: (context, index) {
final currentUser = _users[index];
if (_users.isEmpty) {
return Center(child: Text('No data'));
} else {
return Card(
child: ListTile(
leading: CircleAvatar(
radius: 28,
backgroundImage:
NetworkImage(currentUser['image'].toString()),
),
title: Text(currentUser['email'].toString()),
subtitle: Column(
children: [
Text(currentUser['password'].toString()),
Text(currentUser['name'].toString()),
],
),
),
);
}
}),
);
}
}
嘗試這個:
login() async {
try {
final response = await http.get(Uri.parse(api));
response.body.forEach(e){
if(e["email"].toLowerCase() == _email.toLowerCase()){
_user = json.decode(e)
}
}
} catch (e) {
EasyLoading.showError(e.toString());
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.