我的ListView小部件未显示任何数据

时间:2020-10-20 17:54:26

标签: flutter dart

在我的代码中,我有一个listview小部件,我使用firebase从firestore中向其提供了数据,并试图在屏幕上以文本形式显示数据。但是什么也没有发生,我没有收到任何错误或其他信息。我的页面还是空白。所以谁能告诉我firebase部分或listview中有一个错误。这将非常有帮助。

这是我的代码-

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:social_app/widgets/header.dart';
import 'package:social_app/widgets/progress.dart';

final userRef = FirebaseFirestore.instance.collection("users");

class Timeline extends StatefulWidget {
  @override
  _TimelineState createState() => _TimelineState();
}

class _TimelineState extends State<Timeline> {

  List<dynamic> users = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserById();
    getUsers();
  }

  getUserById() {

  }
  getUsers() async {
    final QuerySnapshot snapshot = await userRef.get();
    setState(() {
      users = snapshot.docs;
    });
      // snapshot.docs.forEach((DocumentSnapshot doc) {
      //   print(doc.data());
      //   print(doc.id);
      //   print(doc.exists);
      // });
  }

  @override
  Widget build(context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.black,
        appBar: header(context, isAppTitle: true),
        body: Container(
          child: ListView(
            children: users.map((user) => Text(user['username'], style: TextStyle(color: Colors.white),)).toList()
          ),
        ),
      ),
    );
  }
}

这是我的跑步成绩-

Performing hot reload...
Syncing files to device sdk gphone x86...
Reloaded 3 of 800 libraries in 817ms.
W/Firestore( 3283): (21.7.1) [Firestore]: Listen for Query(target=Query(users order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
E/flutter ( 3283): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.
E/flutter ( 3283): #0      MethodChannelQuery.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart:105:7)
E/flutter ( 3283): <asynchronous suspension>
E/flutter ( 3283): #1      Query.get (package:cloud_firestore/src/query.dart:178:25)
E/flutter ( 3283): #2      _TimelineState.getUsers (package:social_app/pages/timeline.dart:29:50)
E/flutter ( 3283): #3      _TimelineState.initState (package:social_app/pages/timeline.dart:22:5)
E/flutter ( 3283): #4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
E/flutter ( 3283): #5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 3283): #6      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #7      Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #8      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14)
E/flutter ( 3283): #9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #10     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #11     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14)
E/flutter ( 3283): #12     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #13     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #14     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
E/flutter ( 3283): #15     Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
E/flutter ( 3283): #16     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5)
E/flutter ( 3283): #17     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 3283): #18     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #19     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #20     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
E/flutter ( 3283): #21     Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
E/flutter ( 3283): #22     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5)
E/flutter ( 3283): #23     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 3283): #24     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #25     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #26     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
E/flutter ( 3283): #27     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
E/flutter ( 3283): #28     Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
E/flutter ( 3283): #29     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5)
E/flutter ( 3283): #30     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4791:11)
E/flutter ( 3283): #31     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 3283): #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
E/flutter ( 3283): #35     Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
E/flutter ( 3283): #36     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5)
E/flutter ( 3283): #37     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 3283): #38     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 3283): #39     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 3283): #40     SliverMultiBoxAdaptorElement.updateChild (package:flutter/src/widgets/sliver.dart:1158:36)
E/flutter ( 3283): #41     SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1143:20)
E/flutter ( 3283): #42     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2683:19)
E/flutter ( 3283): #43     SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1136:11)
E/flutter ( 3283): #44     RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:350:23)
E/flutter ( 3283): #45     RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1883:59)
E/flutter ( 3283): #46     PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:915:15)
E/flutter ( 3283): #47     RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1883:14)
E/flutter ( 3283): #48     RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:339:5)
E/flutter ( 3283): #49     RenderSliverMultiBoxAdaptor.insertAndLayoutLeadingChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:455:5)
E/flutter ( 3283): #50     RenderSliverFixedExtentBoxAdaptor.performLayout (package:flutter/src/rendering/sliver_fixed_extent_list.dart:234:32)
E/flutter ( 3283): #51     RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
E/flutter ( 3283): #52     RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:132:12)
E/flutter ( 3283): #53     _RenderSliverFractionalPadding.performLayout (package:flutter/src/widgets/sliver_fill.dart:170:11)
E/flutter ( 3283): #54     RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
E/flutter ( 3283): #55     RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:507:13)
E/flutter ( 3283): #56     RenderViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1561:12)
E/flutter ( 3283): #57     RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1470:20)
E/flutter ( 3283): #58     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1634:7)
E/flutter ( 3283): #59     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:884:18)
E/flutter ( 3283): #60     RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:436:19)
E/flutter ( 3283): #61     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:914:13)
E/flutter ( 3283): #62     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:302:5)
E/flutter ( 3283): #63     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
E/flutter ( 3283): #64     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1055:9)
E/flutter ( 3283): #65     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:971:5)
E/flutter ( 3283): #66     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 3283): #67     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 3283): #68     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 3283): #69     _invoke (dart:ui/hooks.dart:251:10)
E/flutter ( 3283): #70     _drawFrame (dart:ui/hooks.dart:209:3)
E/flutter ( 3283): 
W/Firestore( 3283): (21.7.1) [WatchStream]: (a99c49e) Stream closed with status: Status{code=CANCELLED, description=Disconnecting idle stream. Timed out waiting for new targets., cause=null}.
W/ansh.social_ap( 3283): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)

1 个答案:

答案 0 :(得分:1)

您的代码似乎运行正常。让我解释一下您的问题。

首次创建cloud_firestore时,访问数据的权限在public选项卡中设置为rules。此权限公开30天。之后,firestore将其关闭,您必须定义自己的规则。

在这里,rules是实际的权限,它决定谁可以访问您的数据以及谁不能访问您的数据。因此,暂时您可以通过更改Rules再次公开数据。 请按照以下图片访问Rule标签:

enter image description here

相应地在Rules处更改firestore console。这是link规则文档。

注意: 您可以将Rules公开以用于测试目的,但不要将其永久公开,这会使您的数据不安全。