Flutter Firestore - 获取存储为字符串的 URL 时出现空安全错误

Posted

技术标签:

【中文标题】Flutter Firestore - 获取存储为字符串的 URL 时出现空安全错误【英文标题】:Flutter Firestore - Null Safety Error When Fetching URL stored as String 【发布时间】:2022-01-23 20:34:47 【问题描述】:

在从 Firestore 返回特定的键值对时遇到了似乎是空安全问题。具体来说,尝试从 Firestore 读取 data['thumbnail'] 时会触发错误。 data['thumbnail'] 是图像的 URL,在 Firestore 中存储为字符串。我能够映射文档中的所有其他项目(所有其他项目也是字符串)。如果我将 Firestore 引用换成实际的图像 URL,它就可以工作。我在这里错过了 Firestore 的特定内容吗?

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';

class AllArticles extends StatefulWidget 
  const AllArticles(Key? key) : super(key: key);

  @override
  _AllArticlesState createState() => _AllArticlesState();


class _AllArticlesState extends State<AllArticles> 
  final Stream<QuerySnapshot> _articleStream =
      FirebaseFirestore.instance.collection('all_articles').snapshots();

  @override
  Widget build(BuildContext context) 
    return Container(
        child: StreamBuilder<QuerySnapshot?>(
      stream: _articleStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot?> snapshot) 
        if (snapshot.hasError) 
          return Text('Something went wrong');
        

        if (snapshot.connectionState == ConnectionState.waiting) 
          return Text("Loading");
        

        return Expanded(
            child: ListView(
          children: snapshot.data!.docs.map((DocumentSnapshot? document) 
            Map<String?, dynamic> data =
                document?.data() as Map<String?, dynamic>;
            return Card(
                elevation: 5,
                child: SizedBox(
                  height: 80,
                  child: ListTile(
                    leading: GFAvatar(
                        backgroundImage: NetworkImage(data['thumbnail']),
                        shape: GFAvatarShape.standard),
                    title: Text(
                      data['title'],
                      style: TextStyle(
                          fontSize: 14, overflow: TextOverflow.ellipsis),
                    ),
                    subtitle: Text(data['source']),
                    isThreeLine: true,
                  ),
                ));
          ).toList(),
        ));
      ,
    ));
  

完整的控制台输出

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state:
_StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#285c8):
type 'Null' is not a subtype of type 'String'

The relevant error-causing widget was:
  StreamBuilder<QuerySnapshot<Object?>>
  StreamBuilder:file:///Users/bendepew/Developer/aw/pit-road-reporter/prr_flutter/lib/widgets/AllNascarCard.dart:19:16

When the exception was thrown, this was the stack:
#0      _AllNascarCardState.build.<anonymous closure>.<anonymous closure> (package:prr_flutter/widgets/AllNascarCard.dart:41:59)
#1      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
#2      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
#3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#5      new List.of (dart:core-patch/array_patch.dart:51:28)
#6      ListIterable.toList (dart:_internal/iterable.dart:213:44)
#7      _AllNascarCardState.build.<anonymous closure> (package:prr_flutter/widgets/AllNascarCard.dart:57:14)
#8      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
#9      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
#10     StatefulElement.build (package:flutter/src/widgets/framework.dart:4705:27)
#11     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4588:15)
#12     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#13     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#14     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#15     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#16     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#17     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#18     StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#19     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#20     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#21     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#22     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#23     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#24     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#25     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5626:32)
#26     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6284:17)
#27     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#28     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#29     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#30     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#31     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#32     StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#33     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#35     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#36     ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#37     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#38     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#39     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#40     ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#41     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#42     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5626:32)
#43     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6284:17)
#44     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#45     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#46     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#47     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#48     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#49     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#50     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#51     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#52     ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#53     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#54     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#55     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#56     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#57     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#58     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#59     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#60     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#61     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#62     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#63     StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#64     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#65     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#66     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#67     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#68     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#69     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#70     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#71     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#72     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#73     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#74     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#75     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#76     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#77     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#78     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#79     ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#80     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#81     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#82     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#83     StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#84     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#85     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#86     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#87     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#88     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#89     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#90     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#91     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#92     ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#93     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#94     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#95     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#96     Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#97     StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#98     Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#99     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#100    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#101    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#102    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#103    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#104    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#105    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#106    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#107    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#108    StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#109    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#110    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#111    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#112    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#113    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#114    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#115    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#116    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#117    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#118    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#119    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5626:32)
#120    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6284:17)
#121    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#122    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#123    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#124    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#125    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#126    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#127    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#128    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#129    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#130    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#131    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#132    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#133    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#134    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#135    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#136    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#137    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#138    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#139    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#140    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#141    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#142    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#143    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#144    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#145    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#146    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#147    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#148    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#149    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#150    StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#151    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#152    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#153    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#154    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#155    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#156    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#157    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#158    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#159    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#160    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#161    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#162    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#163    ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#164    _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:111:11)
#165    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#166    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#167    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#168    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#169    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#170    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#171    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#172    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#173    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#174    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#175    ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#176    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#177    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#178    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#179    ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#180    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#181    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#182    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#183    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#184    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#185    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#186    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#187    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#188    StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#189    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#190    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#191    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#192    StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#193    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#194    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#195    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#196    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#197    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#198    ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#199    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#200    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#201    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#202    ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#203    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#204    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#205    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#206    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#207    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#208    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#209    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#210    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#211    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#212    StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#213    Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#214    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#215    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#216    Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#217    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2578:33)
#218    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#219    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#220    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)
#221    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#222    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:863:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════

Another exception was thrown: A RenderFlex overflowed by 99409 pixels on the bottom.
Performing hot reload...                                                
Reloaded 1 of 724 libraries in 1,860ms.

Another exception was thrown: type 'Null' is not a subtype of type 'String'

【问题讨论】:

我检查了你的代码,到目前为止看起来不错。我正在尝试重现该问题,但与此同时,您能否分享图像的数据类型?我的意思是如果它返回一个流、一个承诺或类似的东西?我还想问一下您是如何将以前的代码迁移到空安全的,以及您是否使用过这个reference。最后,您可以添加一个错误处理程序,以防收到的数据可能导致问题,如下所示: data['thumbnail'] =!空值 ?数据['缩略图']:'via.placeholder.com/150' @Alex - 感谢您的帮助。正确,这是一个流。并且 data['thumbmnail'] 是一个存储在 Firestore 中的 URL 作为字符串。几周前我使用 null 安全性开始了这个项目,因此不需要迁移。 【参考方案1】:

正如您之前确认的,数据类型未正确处理,您可以使用await forlisten() 处理Streams,您可以在Asynchronous programming: Streams 中看到它。恐怕我无法在您提供的代码中找到任何这些方法,指出这是收到空安全错误的主要原因,因为没有正确接收数据类型并导致它被处理作为空值。这也发生在其他语言中,这是一个常见的编程错误。

您将需要添加一个方法来接收流和处理错误,所有这些都包含在最后一个链接中。请仔细阅读文档并添加缺少的代码,这将帮助您解决该错误。我会留下这个video 作为补充。

【讨论】:

史诗般的回应@Alex。谢谢!正是需要的。【参考方案2】:

可能是其中一个文档的该字段为空?

【讨论】:

这也是我的假设,但我添加了“?”表示“null”是可接受的返回值,但仍会收到相同的错误。注意:我更新了上面的代码,添加了“?”对于空返回值。

以上是关于Flutter Firestore - 获取存储为字符串的 URL 时出现空安全错误的主要内容,如果未能解决你的问题,请参考以下文章

Flutter/Firestore:“QuerySnapshot”类没有实例获取器“document”

当文档ID等于Flutter中当前记录的用户ID时获取firestore数据?

Flutter 如何优化 Firestore 存储读取以防止大量不必要的读取

Flutter 的 onTap 方法打开存储在 Firestore 中的经纬度

如何通过与 Flutter Cloud Firestore 插件中的日期进行比较来获取记录?

Firestore,Flutter,Async:方法不等待异步方法完成