0

我正在尝试使用包装在如下消费者中的流构建器从 Firestore 中提取数据。但是,由于某种原因,它返回一个空错误。

我很困惑为什么当我使用消费者时这会返回错误但没有它也能正常工作。当我使用消费者时,我是否必须以不同的方式引用集合?

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final _width = MediaQuery
        .of(context)
        .size
        .width;
    final _height = MediaQuery
        .of(context)
        .size
        .height;
return Consumer<CurrentModel>(builder: (context, currentModel, child) {
      return StreamBuilder<DocumentSnapshot>(
        stream: Firestore.instance
            .collection(currentModel.uid)
            .document(currentModel.uid).snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Stack(
children: <Widget>[
                new Container(
                  color: Colors.blue,
                ),
                new Image.network(
                  snapshot.data['imageurl'].toString(),
                  fit: BoxFit.fill,
                ),

堆栈跟踪:

I /颤振(11582):══╡小部件库发现异常╞══════════════════════════════════════════ ═════════════════════════

I/flutter (11582): The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot>(dirty, state:
I/flutter (11582): _StreamBuilderBaseState<DocumentSnapshot, AsyncSnapshot<DocumentSnapshot>>#99271):
I/flutter (11582): The method '[]' was called on null.
I/flutter (11582): Receiver: null
I/flutter (11582): Tried calling: []("imageurl")
I/flutter (11582): 
I/flutter (11582): User-created ancestor of the error-causing widget was:
I/flutter (11582):   Consumer<currentModel>
I/flutter (11582):   file:///Users/alexjasper/AndroidStudioProjects/flutter_myapp/lib/main.dart:48:12
I/flutter (11582): 
I/flutter (11582): When the exception was thrown, this was the stack:
I/flutter (11582): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter (11582): #1      DocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:29:42)
I/flutter (11582): #2      _MyHomePageState.build.<anonymous closure>.<anonymous closure> (package:flutter_myapp/main.dart:61:32)
I/flutter (11582): #3      StreamBuilder.build (package:flutter/src/widgets/async.dart:425:74)
I/flutter (11582): #4      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:125:48)
I/flutter (11582): #5      StatefulElement.build (package:flutter/src/widgets/framework.dart:4047:27)
I/flutter (11582): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3941:15)
I/flutter (11582): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
I/flutter (11582): #8      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2348:33)
I/flutter (11582): #9      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:760:20)
I/flutter (11582): #10     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:280:5)
I/flutter (11582): #11     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1033:15)
I/flutter (11582): #12     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:975:9)
I/flutter (11582): #13     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:891:5)
I/flutter (11582): #17     _invoke (dart:ui/hooks.dart:249:10)
I/flutter (11582): #18     _drawFrame (dart:ui/hooks.dart:207:3)
I/flutter (11582): (elided 3 frames from package dart:async)
I/flutter (11582): 
I/flutter (11582): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (11582): Another exception was thrown: NoSuchMethodError: The method '[]' was called on null.
4

1 回答 1

0

我没有看到您的代码有任何问题。我认为正在发生的事情是你还没有在流上沉没任何价值。也许您可以为 StreamBuilder 设置一个 initialValue 来防止这种情况。

于 2019-09-17T13:13:40.097 回答