首页 > 解决方案 > 如何获取当前用户表单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

标签: arraysfirebaseflutterdartgoogle-cloud-firestore

解决方案


ListsArray飞镖。

您可以检查列表是否包含以下值:

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。


推荐阅读