Map carsMap = {
"vehicles": [
{
"name": "Vehicle1",
"models": ["bus", "plane", "bicycle"]
},
{
"name": "Vehicle2",
"models": ["motorcycle", "sporcar", "plane"]
},
{
"name": "Vehicle3",
"models": ["motorcycle", "plane", "bicycle"]
}
]
};
如上所示,我有车辆列表,我想查询并显示哪个列表包括飞机和摩托车。我可以使用此代码单查询哪个列表包括飞机,但我不能同时查询哪个列表包括飞机和摩托车。
List<Map> updatedList = carsList
.where((m) => m.toString().toLowerCase().contains("plane".toLowerCase()))
.toList();
顺便说一下,飞机和摩托车只是一个例子,这些数据可以根据用户输入而改变。所以我想根据用户输入动态查询。
我怎么能在 where 查询或其他方式中做到这一点。
这是我使用 Firebase 的源代码。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class Home1 extends StatefulWidget {
@override
_Home1State createState() => _Home1State();
}
class _Home1State extends State<Home1> {
final _firestore = FirebaseFirestore.instance;
@override
Widget build(BuildContext context) {
CollectionReference tarifRef = _firestore.collection("vehicles");
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
StreamBuilder<QuerySnapshot>(
stream: tarifRef.snapshots(),
builder: (BuildContext context, AsyncSnapshot asyncsnapshot) {
if (asyncsnapshot.hasError) {
return Center(
child: Text("Error"),
);
} else {
if (asyncsnapshot.hasData) {
List<DocumentSnapshot> listOfDocumentSnapshot =
asyncsnapshot.data.docs;
return Flexible(
child: ListView.builder(
itemCount: listOfDocumentSnapshot.length,
itemBuilder: (context, index) {
return Card(
color: Colors.indigo,
child: ListTile(
title: Text(
"${listOfDocumentSnapshot[index]["name"]}",
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
subtitle: Text(
"${listOfDocumentSnapshot[index]["vehicleList"]}",
style: TextStyle(
fontSize: 15,
color: Colors.white,
),
),
trailing: IconButton(
icon: Icon(
Icons.delete,
color: Colors.white,
),
onPressed: () async {
await listOfDocumentSnapshot[index]
.reference
.delete();
},
),
),
);
},
),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}
},
),
],
),
),
);
}
}