【发布时间】:2021-09-20 01:40:18
【问题描述】:
我对 Flutter 和 firebase 比较陌生,并试图从一组消息中检索消息,每个消息都具有消息文本、发件人等消息属性。 但是,当使用列表视图构建器时,我不能使用 itemcount,因为它说,Future 不能用来代替 int 或“不能无条件调用数据,因为它可能为空”。添加空检查(!)会导致它说它不能用于对象。请帮忙。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MaterialApp(
home: readMessage(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.purple,
)));
}
class readMessage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _readMessageState();
}
}
class _readMessageState extends State<readMessage> {
bool isMe = true;
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('users')
.doc('ItpNRR6uNfl4Mf4qvlIa')
.collection('messages')
.snapshots(),
builder: ((context, snapshot) {
/// This is where I need the listview builder of collection inside the document
}));
}
BoxDecoration messageBubbleMaker(bool isMe){
return BoxDecoration(
borderRadius: BorderRadius.only(topRight: Radius.circular(20), topLeft: Radius.circular(20), bottomRight: isMe? Radius.zero : Radius.circular(20), bottomLeft: !isMe? Radius.zero : Radius.circular(20)),
color: isMe? Colors.black : Colors.blueAccent
);
}
}
这是我的代码。
it supports null safety
【问题讨论】: