flutter - 在 null 上调用了方法“[]”。接收者:空尝试调用:[](“图片”)
问题描述
你好吗?我的代码需要帮助。无论我如何寻找问题,我都找不到原因。我使用 futureBuilder 从 firebase 收集数据以在应用程序中显示它。当我执行说我的代码时:方法'[]'在null上被调用。接收者:null 尝试调用:。
编码
Container(
child: FutureBuilder(
future: roomProduct.RoomData.doc().get(),
builder: (context, snap) {
if (snap.connectionState ==
ConnectionState.done) {
Map<String, dynamic> docs = snap.data.data();
return ListView(
shrinkWrap: true,
children: [
Padding(
padding: const EdgeInsets.all(6.0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(-2,-1),
blurRadius: 5
)
]
),
child: GestureDetector(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(7.0),
child: Image.network(
"${docs['pictures'][0]}",
height: 500,
width: 450,
)
),
],
),
Center(child: Text('${docs['name']} \n',style:
TextStyle(fontSize: 24.0,))),
Center(child: Text('id: ${docs['price']} \n',style:
TextStyle(fontSize: 24.0,))),
],
),
),
),
)
],
);
}
return Container();
},
),
)
错误
The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state:
_FutureBuilderState<DocumentSnapshot>#59eb3):
The method '[]' was called on null.
Receiver: null
Tried calling: []("pictures")
The relevant error-causing widget was:
FutureBuilder<DocumentSnapshot>
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _IncorpoBidPageState.build.<anonymous closure>.<anonymous closure>
(package:MerchantIsland/bidPages/roomPage.dart:122:62)
#2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:773:55)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
...
====================================================================================================
/DynamiteModule( 4330): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 4330): Considering local module providerinstaller:0 and remote module
providerinstaller:0
W/ProviderInstaller( 4330): Failed to load providerinstaller module: No acceptable module found.
Local version is 0 and remote version is 0.
W/DynamiteModule( 4330): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 4330): Considering local module providerinstaller:0 and remote module
providerinstaller:0
W/ProviderInstaller( 4330): Failed to load providerinstaller module: No acceptable module found.
Local version is 0 and remote version is 0.
I/FirebaseAuth( 4330): [FirebaseAuth:] Preparing to create service connection to fallback
implementation
W/System ( 4330): Ignoring header X-Firebase-Locale because its value was null.
解决方案
发生这种情况是因为您正在尝试调用docs['pictures']
,而您尚未docs
在此处发布的代码片段中的任何地方声明。这就是为什么它给出空值错误的原因,因为编译器不知道该变量,因此它将其视为空值。因此,您无法访问 null 变量的“图片”属性。
尝试docs
事先声明变量,在使用它之前,只需检查值是否为 null 或其他内容。
推荐阅读
- c# - Entity Framework Core 2.2.4 插入具有 1:n 关系的项目时出错 - 最佳解决方案
- javascript - 如何在openlayers中优先考虑图像图标?
- java - 圣诞树树干前的间距
- css - ERR_ABORTED 404 (未找到) CSS 样式表 Symfony
- julia - 抑制图例中的某些标签或放置采样标记
- javascript - Paypal 成功交易后将数据存储到 MySQL
- python - 读取文件后从列表中获取特定元素
- html - 如何将 UTF-8 解码为 HTML 标签
- javascript - 材质 ui 更改复选框颜色
- css - 使用 React JS 显示和修改 JSON 数据(显示在表格中)