我正在努力解决这两个错误。
错误:无法无条件访问属性"docs",因为接收器可以为"null"。([church_app] lib\Screens\DevotionalList. dart:23处的unchecked_use_of_nullable_value)
错误:参数'devotional'的值不能为'null',因为其类型不同,但隐式默认值为'null'。([church_app] lib\Screens\DevotionalList. dart:39处缺少参数的默认值)
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class DevotionalList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('devotionals').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text('Error: ${snapshot.error}'),
);
}
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
// Use the .docs property to access the list of documents.
List<DocumentSnapshot> documents = snapshot.data.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (context, index) {
DocumentSnapshot devotional = documents[index];
return DevotionalTile(devotional: devotional);
},
);
},
);
}
}
class DevotionalTile extends StatelessWidget {
final DocumentSnapshot devotional;
DevotionalTile({this.devotional});
@override
Widget build(BuildContext context) {
if (devotional == null) {
return Container();
}
return ExpansionTile(
title: Text(devotional['title']),
subtitle: Text('By ${devotional['author']}'),
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(devotional['body']),
),
],
);
}
}
如有任何帮助,不胜感激。
尝试添加空异常,但仍然遇到相同的错误。
1条答案
按热度按时间t5fffqht1#
在调用
snapshot.data.docs
时,它不能为空,因为您首先检查了hasError
,所以您可以通过显式添加!
将此告知Dart,而不是这样:加上
!
,结果是:这将修正你所面临的两个错误。