arrays - 如何获取当前用户表单firebase的数据?
问题描述
我目前正在使用这样的列表`
所以我遇到的问题是,当用户在主集合“meinprofilsettings”集合中更改名称时,名称没有得到更新。.
这是我现在如何使用它
@override
Widget build(BuildContext context) {
var firestore = FirebaseFirestore.instance;
final user = Provider.of<Userforid>(context);
File _pickedImage;
return Container(
height: 50,
width: widget._width,
child: StreamBuilder<List<ConversationSnippet>>(
stream: DatbaseService.instance.getUserConversations(user.uid),
builder: (_context, AsyncSnapshot _snapshot) {
if (!_snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
var _data = _snapshot.data;
if (_data != null) {
_data.removeWhere((_c) {
return _c.timestamp == null;
});
return ListView.builder(
shrinkWrap: true,
itemCount: _data.length,
itemBuilder: (_context, _index) {
print(allids.length);
return StreamBuilder(
stream: firestore
.collection('meinprofilsettings')
.doc(_data[_index].uid)
.snapshots(),
builder: (context, snapshot) {
log('data: ${_data[_index].uid}');
if (snapshot.hasData) {
dynamic video =
snapshot.data != null ? snapshot.data.data() : {};
print(video.length);
return Text(video['username']);
} else if (snapshot.hasError) {
return const Text('No data avaible right now');
} else {
return Center(child: CircularProgressIndicator());
}
});
},
);
} else {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 250, 0, 0),
child: Text(
"No Conversations Yet",
style: TextStyle(fontSize: 17),
),
);
}
}
},
),
);
}
Ans这是现在的错误
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building StreamBuilder<DocumentSnapshot>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot, AsyncSnapshot<DocumentSnapshot>>#5aafa):
Supported [field] types are [String] and [FieldPath]
'package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart':
Failed assertion: line 69 pos 12: 'field is String || field is FieldPath'
The relevant error-causing widget was
StreamBuilder<DocumentSnapshot>
lib/chatmessenger/recent_conversations_page.dart:77
When the exception was thrown, this was the stack
#2 DocumentSnapshotPlatform.get
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:69
#3 DocumentSnapshot.get
package:cloud_firestore/src/document_snapshot.dart:45
#4 DocumentSnapshot.[]
package:cloud_firestore/src/document_snapshot.dart:52
#5 _RecentConversationspageState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
lib/chatmessenger/recent_conversations_page.dart:123
#6 StreamBuilder.build
package:flutter/…/widgets/async.dart:545
解决方案
Lists
是Array
飞镖。
您可以检查列表是否包含以下值:
allVideoHastags.contains('some hashtag')
但这不是你错误的原因!
正如错误所说,您只想查询具有太多值的 firebase 集合。该whereIn
子句最多支持 10 个值,而您的值allVideoHastags
超过 10 个。
我建议您确保仅将 10 个元素发送到过滤器:
@override
Widget build(BuildContext
....
return ListView.builder(
shrinkWrap: true,
itemCount: _data.length,
itemBuilder: (_context, _index) {
return StreamBuilder(
stream: firestore
.collection('meinprofilsettings')
.where(_data[_index].uid,
whereIn: allVideoHastags.take(10))
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
.....
这也意味着您将在 Firebase 端使用 10 个元素进行过滤,我建议您在客户端使用其余元素进行过滤。
如果您只想获得一个用户,您可以只为该单个 id 的路径获取 snaphost,如下所示:
return ListView.builder(
shrinkWrap: true,
itemCount: _data.length,
itemBuilder: (_context, _index) {
return StreamBuilder(
stream: firestore
.collection('meinprofilsettings')
.doc(_data[_index].uid)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
String username='';
if (snapshot.hasData) {
username= snapshot.data['username'];
}
注意 aDocumentSnaphost
不一样,QuerySnaphot
并且它有不同的 API。
推荐阅读
- ruby-on-rails - replace_gem:加载“sqlite3”Active Record 适配器时出错 - 创建模型时
- database - 连接到 AWS 中的私有 RDS
- java - FirebaseUI RecycleView 搜索不起作用
- spring-batch - 如何停止 Spring Cloud Data Flow 中的任务?
- java - 骆驼 - 伐木后身体变空
- rest - 如何使用linkedin api检索linkedin用户的r_basicprofile权限下列出的所有参数?
- python - 基于模板:使用基于 Slug 的动态 URL 模式通过 Ajax 在 Django 中加载动态内容
- python - 如何将 python eval() 函数用于 tkinter 文本窗口?
- jquery - ajax 搜索引擎不稳定
- apache-kafka - Druid Kafka 摄取的配置