How to use get firebase data to StreamBuilder without Listview - firebase

I need to show my firebase data in deference places. But in the same page.
In every tutorial I followed they use Listview and I need to know there is another way to get data without using the list view

Ya finally I think I found an answer
final uid = FirebaseAuth.instance.currentUser!.uid;
userdata = FirebaseFirestore.instance.collection('/users/$uid/userdata');
return Scaffold(
body: StreamBuilder(
stream: userdata.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
try {
snapshot.data!.docs.map((DocumentSnapshot document) {
data = document.data()! as Map<String, dynamic>;
}).toList();
} catch (e) {
print(e.toString());
}
},
),
);

Expanded(
child: StreamBuilder(
stream:
FirebaseFirestore.instance.collection("posts").snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>
snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
log(snapshot.data!.docs[index].data().toString());
return UserVideos(
snap: snapshot.data!.docs[index].data(),
);
},
);
}
log("${snapshot.stackTrace}stack trace");
log("${snapshot.error}error");
return const Text("Something went wrong");
},
),
),

Related

Adding complete Column Flutter

I am trying to add a complete column containing record but whenever I do it shows null.
The data is being coming from firebase.
To read data
return StreamBuilder<QuerySnapshot>(
stream: otherStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
print('Something went Wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return StreamBuilder<QuerySnapshot>(
stream: otherStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
print('Something went Wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return StreamBuilder<QuerySnapshot>(
stream: otherStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
print('Something went Wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
final controller = ScreenshotController();
final Stream<QuerySnapshot> otherStream =
FirebaseFirestore.instance.collection('lott').snapshots();
final List storedocs = [];
snapshot.data!.docs.map((DocumentSnapshot document) {
Map a = document.data() as Map<String, dynamic>;
storedocs.add(a);
a['id'] = document.id;
}).toList();
TableCell(
child: Center(
child: Text(storedocs[i]['items'].toString(),
style: TextStyle(fontSize: 10.0))),
),
Adding a snap
Any Example would be grateful and helpful thanks

How to retrieve all the Data from Firestore

enter image description here
The Picture above is my Firestore.
I'm trying to retrieve all the data from the Collection UserData.
This is the Code I used. I'm not getting any Red lines or error in Console but its not showing the data of the Collection UserData. Please Help
Container(
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('UserData')
.snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot book = snapshot.data.docs[index];
return ListTile(
leading: Text(book['title']),
title: Text(book['Date']),
subtitle: Text(book['Time']),
);
},
);
}
)
),

Firebase flutter _CastError (Null check operator used on a null value)

The documents on the tax page do not appear on the screen. I am getting the following _CastError (Null check operator used on a null value). Is there anyone who can help?
firestore_db_services.dart
#override
Stream<List<Vergi>> getHizmetler(String currentUserID) {
var snapShot = _firebaseFirestoreDB
.collection("hizmetler")
.doc(currentUserID)
.collection("vergi")
.orderBy("aciklamaDate")
.snapshots();
return snapShot.map(
(vergiListesi) => vergiListesi.docs
.map(
(vergi) => Vergi.fromMap(vergi.data()),
)
.toList(),
);
}
vergi.dart
Expanded(
child: StreamBuilder<List<Vergi>?>(
stream: _userModel.getHizmetler(_currentUser.userID),
builder: (context, snapShot) {
if (snapShot.hasError) {
return Center(
child: MyIndicator(),
);
}
if (snapShot.connectionState == ConnectionState.waiting) {
return Text("Yükleniyor...");
}
List<Vergi>? tumVergiListesi = snapShot.data;
return ListView.builder(
itemCount: tumVergiListesi!.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
tumVergiListesi[index].aciklama.toString(),
),
subtitle: Text(
tumVergiListesi[index].fiyat.toString(),
),
);
},
);
},
),
),
Try returning snapshots from getHizmetler function and not the list you are currently returning:
Stream<QuerySnapshot>getHizmetler(String currentUserID) {
return _firebaseFirestoreDB
.collection("hizmetler")
.doc(currentUserID)
.collection("vergi")
.orderBy("aciklamaDate")
.snapshots();
}
Adjust your StreamBuilder like:
StreamBuilder<QuerySnapshot>(
stream: _userModel.getHizmetler(_currentUser.userID),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
// error handling
return ...;
}
if (snapshot.connectionState == ConnectionState.waiting) {
// progress indicator
return ...;
}
if (snapshot.hasData) {
// here you can do the conversion you like, result will be in:
// snapshot.data!.docs
}
}
)

got an error in flutter require an identifier while using the streambuilder

I have created the widget in flutter app and its connected with the google firebase but i got an error on the StreamBuilder while getting the data document.('quick')
Widget _createBody() {
return StreamBuilder(
stream: Firestore.instance
.collection('notes').document.('quick').snapshots(),
builder: (context, snapshot){
if(snapshot.hasData){
var doc = snapshot.data;
if (doc.exists){
return Text(doc['content']);
}
}
return CircularProgressIndicator();
}
);
}
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('notes').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return Center(
child: Text('Error: ${snapshot.error}'),
);
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: Text('Loading...'),
);
default:
return new ListView(
children: snapshot.data.documents.map((
DocumentSnapshot document) {
return ListTile(
title: Text(document['content']),
);
}).toList(),
}
}
};
It should be something like this if you're fetching a single document.
Widget _createBody() {
return StreamBuilder(
stream: Firestore.instance
.collection('notes')
.document('quick')
.get()
.snapshots(),
builder: (context, snapshot){
if(snapshot.hasData){
var doc = snapshot.data;
if (doc.exists){
return Text(doc['content']);
}
}
return CircularProgressIndicator();
}
);
}
If this doesn't work, you can always change it like so:
Firestore.instance
.collection('notes')
.document('quick')
.get()
.then((DocumentSnapshot ds) {
// use ds as a snapshot
});

StreamBuilder with Firestore loading vs no data

This feels a bit like a bug to me but I'm not sure. I have simple Firestore query in a Flutter app that I'm running through a Stream Builder. Looks something like this -
StreamBuilder(
stream: Firestore.instance.collection('users').where('toNumber', isEqualTo: '123').snapshots();
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else {
if (snapshot.data.length == 0) {
return Center(child: Text("No Data"));
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) =>
_buildList(
context, snapshot.data[index]),
);
}
}
)
The problem is that if the query returns no results then all you see in a continuous progress indicator. I would of thought hasData would return true but it seems this is not the case. I've also tried using ConnectionState but this always returns .waiting.
In this situation how would you differentiate between a query which is loading and a query which doesn't return any results?
Try the following :
if (!snapshot.hasData) {
if (snapshot.data.length == 0) {
return Center(child: Text("No Data"));
}
else{
return Center(
child: CircularProgressIndicator(),
);
}
}
Here is my build method. I've just tested it, it's working.
#override
Widget build(BuildContext context) {
return StreamBuilder(
stream: firestore
.collection('todos')
.where('state', isEqualTo: false)
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
print(snapshot.connectionState);
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return snapshot.data.documents.isNotEmpty
? ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (_, index) {
return Text(snapshot.data.documents[index].data["title"]);
})
: Text('No Data');
});
}
This prints:
I/flutter ( 6216): ConnectionState.waiting
I/flutter ( 6216): ConnectionState.active

Resources