【问题标题】:How to get data of current user form firebase?如何获取当前用户表单firebase的数据?
【发布时间】:2021-07-18 23:01:32
【问题描述】:

我目前正在使用这样的列表 `

这是这个系列

所以我遇到的问题是,当用户在主集合“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),
                ),
              );
            }
          }
        },
      ),
    );
  }

这是现在的错误

════════ 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

【问题讨论】:

    标签: arrays firebase flutter dart google-cloud-firestore


    【解决方案1】:

    Lists 是 dart 中的 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'];
                        }
    
    

    注意DocumentSnaphostQuerySnaphot 不同,并且它具有不同的 API。

    【讨论】:

    • 是的,但我该如何解决这个问题?
    • 为什么我不能使用 aryraycontains 来代替 10 个元素的限制?
    • 你能解释一下你想如何使用contains吗?您要下载所有数据并在本地进行过滤吗?
    • .snahsots 是为类型 doc 定义的还是错误的?我需要一个集合来使用快照还是我错了?
    • Heys Tarik 我解决了问题,它是这个 _data[_index].url 所以我使用的是快照 [_index].uid 而不是那个。是的,谢谢男人。但也许你现在可以帮助我解决另一个问题。如果您这样做,我会向您发送链接会很高兴。再次感谢一千次​​pan>
    猜你喜欢
    • 2021-03-05
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    相关资源
    最近更新 更多