从 firebase firestore 集合中获取文档并添加 List<Map<String, dynamic>>
get documents from firebase firestore collection and add List<Map<String, dynamic>>
我如何从 firebase firestore 集合中获取文档并将它们添加到 _allUsers 而不是现有值中,我最近在升级 flutter 后遇到了这个问题,在此之前一切正常,我试图查看文档,但我无法解决它
List<listCard> orderWidgets = [];
void get_allUsers(){
FirebaseFirestore.instance
.collection('users')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
final orderName = doc['name'];
final orderAddress = doc['address'];
final orderTime = doc['time'];
final orderWidgetshape = Card(
name: orderName ?? "default value",
location: orderAddress ?? "default value",
time: orderTime ?? "default value",
);
orderWidgets.add(orderWidgetshape);
});
});
}
列表
final List<Map<String, dynamic>> _allUsers = [
{"id": 1, "name": "Andy", "date": '2022-01-25 10:14'},
{"id": 2, "name": "Aragon", "date": '2022-01-02 10:14'},
{"id": 3, "name": "Bob", "date": '2022-01-20 10:14'},
{"id": 4, "name": "Barbara", "date": '2022-01-20 10:14'},
{"id": 5, "name": "Candy", "date": '2022-01-18 10:14'},
{"id": 6, "name": "Colin", "date": '2022-01-24 10:14'},
{"id": 7, "name": "Audra", "date": '2022-01-23 10:14'},
{"id": 8, "name": "Banana", "date": '2022-01-22 10:14'},
{"id": 9, "name": "Caversky", "date": '2022-01-30 10:14'},
{"id": 10, "name": "Becky", "date": '2022-01-28 10:14'},
];
// This list holds the data for the list view
List<Map<String, dynamic>> _foundUsers = [];
void initState() {
super.initState();
setState(() {
_foundUsers = _allUsers;
});
}
列表视图和过滤按钮
@override
Widget build(BuildContext context) {
final orientation = MediaQuery.of(context).orientation;
return Scaffold(
appBar: AppBar(
elevation: 0.0,
bottomOpacity: 0.0,
),
drawer: AppDrawer(),
body: ListView(
children: [
Row(mainAxisSize: MainAxisSize.max, children: [
Expanded(
child: Container(
height: orientation == Orientation.portrait ? 70 : 70,
// width: 170.0,
child: ReusableFilterChipHomeVisits(
onPress: () {
setState(() {
_foundUsers = _allUsers.where((element) {
DateTime tempDate = new DateFormat("yyyy-MM-ddhh:mm").parse(element['date'] );
return now_2days.isBefore(tempDate);
}).toList();
selectedButton = Btn.last2day;
});
},
cardChild: Text(
'آخر يومين',
textAlign: TextAlign.center,
style: TextStyle(
color: selectedButton == Btn.last2day
? KWhiteColor
: KInActiveColor,
fontSize: 19.0,
fontFamily: 'Cairo-Italic',
fontWeight: FontWeight.w600,
),
),
colour: selectedButton == Btn.last2day
? KInActiveColor
: KWhiteColor,
),
),
),
SizedBox(height: 25.0),
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('visithomeorders').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Center(
child: Container(
child: CircularProgressIndicator(
backgroundColor: KInActiveColor,
),
),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return SingleChildScrollView(
child: Column(
children: [
_foundUsers.isNotEmpty
? ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: _foundUsers.length,
itemBuilder: (context, index) =>
Card(
key: ValueKey(_foundUsers[index]["id"]),
color: Colors.amberAccent,
elevation: 4,
margin: const EdgeInsets.symmetric(
vertical: 10),
child: ListTile(
leading: Text(
_foundUsers[index]["id"].toString(),
style: const TextStyle(fontSize: 24),
),
title: Text(_foundUsers[index]['name']),
subtitle: Text(
'${_foundUsers[index]["date"]
.toString()}'),
),
),
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
]
),
);
})
],
),
// bottomNavigationBar: MyBottomBar(),
);
}
}
我找到了答案:
我按照文档中的说明使用了此功能
get_allUsers(){
FirebaseFirestore.instance
.collection('visithomeorders')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
_allUsers.add(doc.data());
});
});
}
然后
void initState() {
super.initState();
setState(() {
_foundUsers = _allUsers;
get_allUsers();
});
}
最后我使用了 Streambuilder 中的列表
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('visithomeorders').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Center(
child: Container(
child: CircularProgressIndicator(
backgroundColor: KInActiveColor,
),
),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return SingleChildScrollView(
child: Column(
children: [
_foundUsers.isNotEmpty
? ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: _foundUsers.length,
itemBuilder: (context, index) =>
listCard(
name: _foundUsers[index]['name'] ?? "default value",
time: _foundUsers[index]['time'] ?? "default value",
date: _foundUsers[index]['date'] ?? "default value",
)
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
]
),
);
})
我如何从 firebase firestore 集合中获取文档并将它们添加到 _allUsers 而不是现有值中,我最近在升级 flutter 后遇到了这个问题,在此之前一切正常,我试图查看文档,但我无法解决它
List<listCard> orderWidgets = [];
void get_allUsers(){
FirebaseFirestore.instance
.collection('users')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
final orderName = doc['name'];
final orderAddress = doc['address'];
final orderTime = doc['time'];
final orderWidgetshape = Card(
name: orderName ?? "default value",
location: orderAddress ?? "default value",
time: orderTime ?? "default value",
);
orderWidgets.add(orderWidgetshape);
});
});
}
列表
final List<Map<String, dynamic>> _allUsers = [
{"id": 1, "name": "Andy", "date": '2022-01-25 10:14'},
{"id": 2, "name": "Aragon", "date": '2022-01-02 10:14'},
{"id": 3, "name": "Bob", "date": '2022-01-20 10:14'},
{"id": 4, "name": "Barbara", "date": '2022-01-20 10:14'},
{"id": 5, "name": "Candy", "date": '2022-01-18 10:14'},
{"id": 6, "name": "Colin", "date": '2022-01-24 10:14'},
{"id": 7, "name": "Audra", "date": '2022-01-23 10:14'},
{"id": 8, "name": "Banana", "date": '2022-01-22 10:14'},
{"id": 9, "name": "Caversky", "date": '2022-01-30 10:14'},
{"id": 10, "name": "Becky", "date": '2022-01-28 10:14'},
];
// This list holds the data for the list view
List<Map<String, dynamic>> _foundUsers = [];
void initState() {
super.initState();
setState(() {
_foundUsers = _allUsers;
});
}
列表视图和过滤按钮
@override
Widget build(BuildContext context) {
final orientation = MediaQuery.of(context).orientation;
return Scaffold(
appBar: AppBar(
elevation: 0.0,
bottomOpacity: 0.0,
),
drawer: AppDrawer(),
body: ListView(
children: [
Row(mainAxisSize: MainAxisSize.max, children: [
Expanded(
child: Container(
height: orientation == Orientation.portrait ? 70 : 70,
// width: 170.0,
child: ReusableFilterChipHomeVisits(
onPress: () {
setState(() {
_foundUsers = _allUsers.where((element) {
DateTime tempDate = new DateFormat("yyyy-MM-ddhh:mm").parse(element['date'] );
return now_2days.isBefore(tempDate);
}).toList();
selectedButton = Btn.last2day;
});
},
cardChild: Text(
'آخر يومين',
textAlign: TextAlign.center,
style: TextStyle(
color: selectedButton == Btn.last2day
? KWhiteColor
: KInActiveColor,
fontSize: 19.0,
fontFamily: 'Cairo-Italic',
fontWeight: FontWeight.w600,
),
),
colour: selectedButton == Btn.last2day
? KInActiveColor
: KWhiteColor,
),
),
),
SizedBox(height: 25.0),
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('visithomeorders').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Center(
child: Container(
child: CircularProgressIndicator(
backgroundColor: KInActiveColor,
),
),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return SingleChildScrollView(
child: Column(
children: [
_foundUsers.isNotEmpty
? ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: _foundUsers.length,
itemBuilder: (context, index) =>
Card(
key: ValueKey(_foundUsers[index]["id"]),
color: Colors.amberAccent,
elevation: 4,
margin: const EdgeInsets.symmetric(
vertical: 10),
child: ListTile(
leading: Text(
_foundUsers[index]["id"].toString(),
style: const TextStyle(fontSize: 24),
),
title: Text(_foundUsers[index]['name']),
subtitle: Text(
'${_foundUsers[index]["date"]
.toString()}'),
),
),
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
]
),
);
})
],
),
// bottomNavigationBar: MyBottomBar(),
);
}
}
我找到了答案: 我按照文档中的说明使用了此功能
get_allUsers(){
FirebaseFirestore.instance
.collection('visithomeorders')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
_allUsers.add(doc.data());
});
});
}
然后
void initState() {
super.initState();
setState(() {
_foundUsers = _allUsers;
get_allUsers();
});
}
最后我使用了 Streambuilder 中的列表
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('visithomeorders').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Center(
child: Container(
child: CircularProgressIndicator(
backgroundColor: KInActiveColor,
),
),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return SingleChildScrollView(
child: Column(
children: [
_foundUsers.isNotEmpty
? ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: _foundUsers.length,
itemBuilder: (context, index) =>
listCard(
name: _foundUsers[index]['name'] ?? "default value",
time: _foundUsers[index]['time'] ?? "default value",
date: _foundUsers[index]['date'] ?? "default value",
)
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
]
),
);
})