从Firestore返回特定的键值对时,遇到似乎是Null安全问题。具体来说,当尝试从Firestore读取data['thumbnail']
时,会触发该错误。data['thumbnail']
是图像的URL,在Firestore中以字符串形式存储。我能够映射到文档中的所有其他项(所有其他项也是字符串(。如果我把Firestore引用换成一个实际的图像URL,它就可以工作了。我是不是错过了火店特有的东西?
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'
正如您之前确认的,数据类型没有得到正确处理,您可以使用await for
或listen()
处理Streams,您可以在异步编程:Streams中看到它。恐怕我在您提供的代码中找不到这些方法中的任何一个,指出这是收到null安全错误的主要原因,因为数据类型没有正确接收,导致它被视为null值。这种情况在其他语言中也会发生,这是一个常见的编程错误。
您需要添加一个方法来接收流并处理错误,所有这些都包含在最后一个链接中。请仔细阅读文档并添加缺失的代码,这将帮助您解决该错误。我将留下这段视频作为补充。
可能是其中一个文档的字段为null?