你好吗?我的代码需要帮助。无论我如何寻找问题,都找不到原因。我使用 futureBuilder 从 firebase 收集数据以在应用程序中显示它。当我执行说我的代码时:方法 '[]' 在 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.
答案 0 :(得分:0)
发生这种情况是因为您尝试调用 docs['pictures']
而您没有在此处发布的代码片段中的任何地方声明 docs
。这就是为什么它会给出空值错误的原因,因为编译器不知道该变量,因此它将其视为空值。因此,您无法访问空变量的“图片”属性。
尝试事先声明 docs
变量并在使用它之前,只需检查该值是否为 null 或其他值。