Flutter Future空的
向大家问好,并感谢您提前抽出时间。 作为这里的一个非常新的成员,我恳请不要对我太严厉,并担心我是否违反了此时此刻我没有意识到的任何规则。
情况:我得到了 flutter 代码来增强功能。预先存在的代码正在从 firestore 数据库获取数据。我被要求增强代码以添加用户管理(管理功能)。所有涉及的人员都是flutter新手,所以所有代码基本上都是通过网络示例完成的。
问题: 到目前为止还可以,但是一旦我在查询快照上调用未来函数,我就不会得到任何用户数据。我尝试了不同的方法,但没有一个真正有效(例如,通过 for 循环循环快照、添加 Firestore 索引器、添加规则等)。
在下文中,我将向您介绍 Firestore 结构以及检索 Firestore 的最新尝试。数据并发送到列表视图构建器:
这是代码:
final _auth = FirebaseAuth.instance;
CollectionReference _collectionRef =
FirebaseFirestore.instance.collection('users');
class _UserlistScreenState extends State<UserlistScreen> {
final currentUser = _auth.currentUser.email;
@override
void initState() {
getUserList();
print(traceCount++);
super.initState();
}
var traceCount = 0;
var snapshotData;
var itemCount;
var userList;
Future<QuerySnapshot> getUserList() async {
return userList = await _collectionRef
.doc('settings')
.collection('userSettings')
//.where('cid', isEqualTo: globals.CID)
.get();
}
/*Function Cycle() {
for (var doc in userList.docs) {
print(doc.data);
}
}*/
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Hero(
tag: 'logo',
child: Image.asset(
'assets/rgb.png',
height: MediaQuery.of(context).size.height * 0.05,
fit: BoxFit.cover,
),
),
actions: [],
centerTitle: true,
elevation: 4,
),
body: BackgroundContainer(
child: Column(
children: [
Expanded(
child: FutureBuilder(
future: getUserList(),
builder:
(BuildContext context, AsyncSnapshot querySnapshot) {
if (querySnapshot.hasData) {
print(userList.docs); // test
/*return ListView(children: getUser(querySnapshot));*/ // also test
return ListView.builder(
itemCount: userList.docs.length,
itemBuilder: (BuildContext context, index) {
var xname = userList.docs[index]['name'].toString();
var xemail = userList.docs[index]['email'].toString();
final List<Map> _tableData = [
{'id': 'Name:', 'content': xname},
{'id': 'Email:', 'content': xemail},
];
return ListTile(
enabled: true,
selected: false,
tileColor: Colors.black,
leading: Icon(Icons.person),
title: Text('Name'),
subtitle: Text('Email'),
/*title: Text("Name: " +
// userList.data.docs[index].get('name')),
userList.data.docs[index]['name'].toString()),
subtitle: Text("Email: " +
userList.data.docs[index].get('email')),
// userList.data.docs[index]['email'].toString()),*/
onTap: () {
showModalBottomSheet(
context: context,
builder: (context) => Container(
height: 500,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Table(
border: TableBorder(
horizontalInside:
BorderSide(width: 1)),
children: _tableData.map((data) {
return TableRow(
children: [
Container(
padding: EdgeInsets.all(8.0),
child: Text(data['id']),
),
Container(
padding: EdgeInsets.all(8.0),
child: Text(data['content']),
),
],
);
}).toList(),
),
),
),
);
},
);
},
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}),
),
],
),
),
),
);
}
}
Firestore规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
FireStore索引 在此处输入图像描述
我感觉代码盲,无法找到我所依赖的基本错误。非常感谢帮助。
亲切的问候
Greetings everyone and thank you for your time upfront.
As a very new member here i kindly ask to be not too harsh with me and my concern if i break any rule i wasnt aware of in this moment.
Situation: i was given flutter code to enhance with functions. The pre-existing code is getting data from a firestore database. I was asked to enhance the code to add user-management (admin functions). All persons involved are new to flutter so all code is basically done by examples from the net.
Problem:
So far so ok, but as soon as i call a future function on a querysnapshot, i dont get any userdata. I tried different approaches but none actually worked (e.g. cycling though the snapshot via for-loop, added a Firestore Indexer, added the rules, etc.)
In the following i give you an oversight over the firestore structure and the latest attempt to retrieve the data and sending to a listview builder:
And this is the code:
final _auth = FirebaseAuth.instance;
CollectionReference _collectionRef =
FirebaseFirestore.instance.collection('users');
class _UserlistScreenState extends State<UserlistScreen> {
final currentUser = _auth.currentUser.email;
@override
void initState() {
getUserList();
print(traceCount++);
super.initState();
}
var traceCount = 0;
var snapshotData;
var itemCount;
var userList;
Future<QuerySnapshot> getUserList() async {
return userList = await _collectionRef
.doc('settings')
.collection('userSettings')
//.where('cid', isEqualTo: globals.CID)
.get();
}
/*Function Cycle() {
for (var doc in userList.docs) {
print(doc.data);
}
}*/
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Hero(
tag: 'logo',
child: Image.asset(
'assets/rgb.png',
height: MediaQuery.of(context).size.height * 0.05,
fit: BoxFit.cover,
),
),
actions: [],
centerTitle: true,
elevation: 4,
),
body: BackgroundContainer(
child: Column(
children: [
Expanded(
child: FutureBuilder(
future: getUserList(),
builder:
(BuildContext context, AsyncSnapshot querySnapshot) {
if (querySnapshot.hasData) {
print(userList.docs); // test
/*return ListView(children: getUser(querySnapshot));*/ // also test
return ListView.builder(
itemCount: userList.docs.length,
itemBuilder: (BuildContext context, index) {
var xname = userList.docs[index]['name'].toString();
var xemail = userList.docs[index]['email'].toString();
final List<Map> _tableData = [
{'id': 'Name:', 'content': xname},
{'id': 'Email:', 'content': xemail},
];
return ListTile(
enabled: true,
selected: false,
tileColor: Colors.black,
leading: Icon(Icons.person),
title: Text('Name'),
subtitle: Text('Email'),
/*title: Text("Name: " +
// userList.data.docs[index].get('name')),
userList.data.docs[index]['name'].toString()),
subtitle: Text("Email: " +
userList.data.docs[index].get('email')),
// userList.data.docs[index]['email'].toString()),*/
onTap: () {
showModalBottomSheet(
context: context,
builder: (context) => Container(
height: 500,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Table(
border: TableBorder(
horizontalInside:
BorderSide(width: 1)),
children: _tableData.map((data) {
return TableRow(
children: [
Container(
padding: EdgeInsets.all(8.0),
child: Text(data['id']),
),
Container(
padding: EdgeInsets.all(8.0),
child: Text(data['content']),
),
],
);
}).toList(),
),
),
),
);
},
);
},
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}),
),
],
),
),
),
);
}
}
Firestore rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
FireStore Index
enter image description here
I feel code-blinded and cant find the basic mistake i am relying on. Help much appreciated.
Kind regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
settings
字段是一个集合而不是文档,因此代码中的这两行指的是您想要的数据:
它们应该相反,例如:
前面的代码将返回文档“userSettings ”
your
settings
field is a collection not a document,so these two lines in your code are referring to the data that you want:
they should be the opposite, like:
the previous code would return the document "userSettings"