Reputation: 311
I have created createRecord() method:
CollectionReference addBlog = FirebaseFirestore.instance.collection('blogAddress');
Future<void> createRecord() {
// Call the user's CollectionReference to add a new user
return addBlog
.add({
'blogAddress': IfUserProfile.blog, // Stokes and Sons
})
.then((value) => print("Blog Added"))
.catchError((error) => print("Failed to add Blog: $error"));
}
which create the record from a textformField:
onChanged: (value) {
IfUserProfile.blog = value;
},
it work correctly it adds on Firestore correctly the collection:
Now I am trying to get this data into another screen and I call the instance:
CollectionReference blogAddress =
FirebaseFirestore.instance.collection('blogAddress');
and I wrap the Column where I have the data with FutureBuilder:
FutureBuilder<DocumentSnapshot>(
future: blogAddress.doc().get(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data.data();
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 30,
),
Text("Full Name: ${data['blogAddress']}"),
but I cant the data I get:
The method '[]' was called on null. Receiver: null Tried calling:
Upvotes: 0
Views: 102
Reputation: 317798
snapshot.data.data()
is returning null. That happens when you request a document that doesn't exist. Your code blogAddress.doc().get()
is never going to get a document, because doc()
with no arguments generates a reference to a random document ID that doesn't exist yet. If you want a specific document, you should pass the document ID to doc()
.
blogAddress.doc("the-document-ID-you-want").get()
Upvotes: 1
Reputation: 5993
Try this,
body: StreamBuilder(
stream: Firestore.instance
.collection('blogAddress')
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.data.toString()=='null')
return Text('');
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
if(snapshot.data.documents.length<1)
return SizedBox.shrink();
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text('Loading...');
default:
Print("snapshotData:- "+snapshot.data.documents.toString());
return new ListView(
children: snapshot.data.documents
.map((DocumentSnapshot document) {
Print("documentAll :-"+document.toString());
Print("document blogAddress:- "+ document['blogAddress'].toString());
return Text(document['blogAddress'].toString());
}).toList(),
);
}
},
)
Upvotes: 0