【问题标题】:The property can't be unconditionally accessed because the receiver can be 'null'...?不能无条件访问该属性,因为接收者可以是\'null\'...?
【发布时间】:2022-08-15 01:42:41
【问题描述】:
嘿伙计们,我有一个错误,代码如下:
import \'package:cloud_firestore/cloud_firestore.dart\';
import \'package:firebase_core/firebase_core.dart\';
import \'package:flutter/material.dart\';
class ChatScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection(\'chats/RMxQeDVKeYPOW940bWCH/messages/\')
.snapshots(),
builder:(ctx, snapshot){
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
final docs = snapshot.data.docs;
return ListView.builder(
itemCount: docs.length,
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8),
child: Text(docs[index][\'text\']),
),
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
FirebaseFirestore.instance
.collection(\'chats/RMxQeDVKeYPOW940bWCH/messages/\')
.snapshots()
.listen((event) {
event.docs.forEach((element) {
print(element[\'text\']);
});
});
},
),
);
}
}
现在问题出在:
final docs = snapshot.data.docs;
它说:
The property \'docs\' can\'t be unconditionally accessed because the receiver can be
\'null\'.
它只是在快照数据之后的文档中出现错误,所以有人可以帮我吗?
谢谢。
标签:
flutter
dart-null-safety
【解决方案1】:
除了为 StreamBuilder 添加类型之外,您所做的一切都很完美。仅空安全无法解决您的问题。这是一段代码,我只在 Scaffold Widget 的主体中做了一些改动。
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('chats/RMxQeDVKeYPOW940bWCH/messages/')
.snapshots(),
builder:(ctx, snapshot){
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
if(snapshot.hasData) {
final docs = snapshot.data!.docs;
return ListView.builder(
itemCount: docs.length,
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8),
child: Text(docs[index]['text']),
),
);
}
else {
return Text("Something Went wrong");
}
},
)
【解决方案2】:
正如错误消息所说,属性docs 不能无条件访问,因为接收者可以
null。
var docs = snapshot?.data?.docs;
return ListView.builder(
itemCount: docs?.length ?? 0,
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8),
child: Text(docs?[index]['text'] ?? ''),
),
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
if(event.docs =! null)
FirebaseFirestore.instance
.collection('chats/RMxQeDVKeYPOW940bWCH/messages/')
.snapshots()
.listen((event) {
event.docs.forEach((element) {
print(element['text']);
});
});
【解决方案3】:
你需要做出改变就在这条线上
builder: (context, snapshot) 到 builder: (context, AsyncSnapshot snapshot)
然后使用
snapshot.data as snapshot.data!。
【解决方案4】:
- 为您的构建器设置类型
- 创建变量并分配
snapshot.data 和!
- 使用不带任何
?、! 的变量。与[] 一样简单的列表
FutureBuilder<List<Orders>>( // 1
future: futureOrders,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
} else {
final List<Orders> orders = snapshot.data!; // 2
return ListView.builder(
itemCount: orders.length, // 3
itemBuilder: (context, index) => _cartItemWidget(
id: orders[index].id, // 3
cost: orders[index].cost, // 3
date: orders[index].createdAt)); // 3
}
},
)