I watched the Tutorial from Google about Firestore, but i came up with problems about “null exception”. I imported the package and all, but i dont get the answer.
Main Page
StreamBuilder(
stream: FirebaseFirestore.instance.collection("testCity").snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Bitte Warten");
}
return ListView.separated(
padding: const EdgeInsets.fromLTRB(24, 16, 24, 24),
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: snapshot.data.docs.length,
itemBuilder: (context, int index) {
return NormalCard(database: snapshot.data.docs[index]);
},
separatorBuilder: (BuildContext context, int index)
=> const Divider(
height: 32,
thickness: 4,
color: Color(0xFF484848),
),
);
},
)
Normalcard Widget
class NormalCard extends StatefulWidget {
const NormalCard({Key? key, required this.database}) : super(key: key);
final DocumentSnapshot database;
@override
State<NormalCard> createState() => _NormalCardState();
}
class _NormalCardState extends State<NormalCard> {
@override
Widget build(BuildContext context) {
DocumentSnapshot database = widget.database;
return LayoutBuilder(
...
);
}
}
Following Errors occured:
at Problems Tab:
“The property ‘docs’ can’t be unconditionally accessed because the receiver can be ‘null’.”
at run:
lib/Pages/page_startseite.dart:80:40: Error: Property ‘docs’ cannot be accessed on ‘QuerySnapshot<Map<String, dynamic>>?’ because it is potentially null.
- ‘QuerySnapshot’ is from ‘package:cloud_firestore/cloud_firestore.dart’ (‘../../../AppData/Local/Pub/Cache/hosted/pub.dev/cloud_firestore-4.14.0/lib/cloud_firestore.dart’).
- ‘Map’ is from ‘dart:core’.
Try accessing using ?. instead.
itemCount: snapshot.data.docs.length,
^^^^
lib/Pages/page_startseite.dart:82:59: Error: Property ‘docs’ cannot be accessed on ‘QuerySnapshot<Map<String, dynamic>>?’ because it is potentially null. - ‘QuerySnapshot’ is from ‘package:cloud_firestore/cloud_firestore.dart’ (‘../../../AppData/Local/Pub/Cache/hosted/pub.dev/cloud_firestore-4.14.0/lib/cloud_firestore.dart’).
- ‘Map’ is from ‘dart:core’.
Try accessing using ?. instead.
return NormalCard(database: snapshot.data.docs[index]);
^^^^
lib/Pages/page_exploreseite.dart:112:40: Error: Required named parameter ‘database’ must be provided.
return const NormalCard();
^
lib/Widgets/card_restaurantNormal.dart:5:9: Context: Found this candidate, but the arguments don’t match.
const NormalCard({Key? key, required this.database}) : super(key: key);
^^^^^^^^^^
Can anyone help me with this problems so i can implement my Database from Firestore as Backend of my Project?
Nikolai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.