I am trying to create a list of unique "events" in my app. I have created a couple of functions to extract the data from firebase:
// event list from snapshot
List<String> _eventsFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map(
(doc) {
return doc['event'].toString() ?? '';
},
).toList();
}
//get events data
Stream<List<String>> get events {
return productCollection.snapshots().map(_eventsFromSnapshot);
}
I then want to build my list view in another screen. I have implemented my StreamProvider in the root page of my homescreen:
class OurHomePage extends StatefulWidget {
#override
_OurHomePageState createState() => _OurHomePageState();
}
class _OurHomePageState extends State<OurHomePage> {
#override
Widget build(BuildContext context) {
return StreamProvider<List<Product>>.value(
value: OurDatabase().products.handleError((e) {
print(e.toString());
}),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
alignment: Alignment.center,
child: Column(
children: [
OurHeadline(),
AllCards(),
HowItWorks(),
],
),
),
),
),
);
}
}
And then I create a function to return the list of Strings and use that in my stateless widget:
class AllCards extends StatelessWidget {
#override
Widget build(BuildContext context) {
final List<String> uniqueEventList = getListOfEvents(context);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [Text('Browse all Cards'), Text('Shop All')],
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
SizedBox(
height: 125,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: uniqueEventList.length,
itemBuilder: (context, i) {
return Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 160.0,
child: Center(
child: Text(uniqueEventList[i]),
),
);
},
),
)
],
),
),
);
}
List<String> getListOfEvents(BuildContext context) {
final uniqueEvents = Provider.of<List<Product>>(context);
final List<String> list = [];
for (var item in uniqueEvents) {
list.add(item.event);
}
return list.toSet().toList();
}
}
The problem is that whenever I switch pages, for a split second I get this message and an error appears:
The getter 'iterator' was called on null.
Receiver: null
Tried calling: iterator
Which indicates to me that I need to use some sort of async functionality to wait for the events data to finish loading, but is there a simple way to do this without going for something like a Future builder?
Any help would be appreciated!
Related
I'm trying to get documents from a collection with the method FireBaseFirestore.instance.collection("users").where("name", isEqualTo : "something").get() which used to have a return type of QuerySnapshot.
My goal is to make a ListView or anything that can display like a ListView the result(s) of this request.
I have these functions :
This one is to get the documents with the where method
class DataBaseMeth {
getUserByUsername(String username) async{
return fsInstance.collection("users").where("name", isEqualTo: username).get();
}
}
This one is the widget with the result :
class SearchResultTile extends StatelessWidget {
final String username;
const SearchResultTile({
Key? key,
required this.username,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: Row(
children: [
Column(
children: [
Text(
username,
),//username
],
),
)
],
),
);
}
}
And finally the class of the page :
class SearchPage extends StatefulWidget {...}
class _SearchPageState extends State<SearchPage> {
DataBaseMeth dataBaseMethods = DataBaseMeth();
TextEditingController usernameSearchController = TextEditingController();
QuerySnapshot searchSnapshot; //the only way the code run is to replace the type by dynamic
initSearch(){
dataBaseMethods.getUserByUsername(usernameSearchController.text)
.then((result){
setState((){
searchSnapshot = result;
print("result : $searchSnapshot");
//print("result : ${searchSnapshot.docs[1].data.toString()}");
});
});
}
Widget searchList(){
return searchSnapshot != null ?
ListView.builder(
shrinkWrap: true,
itemCount: searchSnapshot.docs.length,
itemBuilder: (context, index) {
return SearchResultTile(
username: searchSnapshot.docs[index].data.toString(),
personalMessage: "personalMessage");
}
)
:
Container();
}
#override
void initState() {
searchList();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: const MainAppBar(titleText: 'Search truc', mainPage: true),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
child: Column(
children: [
Row(
children: [
Expanded(
child: TextField(
controller: usernameSearchController,
decoration: textFieldInputDecoration("search username..."),
style: whiteText(),
),
),
IconButton(
onPressed: () {
initSearch();
},
icon: const Icon(Icons.search_outlined),
color: const Color(0xFFFFFFFF),
highlightColor: Colors.deepPurple,
splashColor: const Color(0xFF3A206B),
tooltip: "Search",
),
],
),
searchList()
],
),
),
);
}
}
The result of the print of searchSnapshot (when I put it on dynamic) is :
I/flutter (31401): result : Instance of '_JsonQuerySnapshot'
And nothing appears when I tap on the button.
Your fsInstance.collection("users").where("name", isEqualTo: username).get() returns a Future<QuerySnapshot> not a QuerySnapshot, so that's why you can't assign it to QuerySnapshot searchSnapshot. You can assign it to Future<QuerySnapshot> searchSnapshot though.
That also means that if you want to use it in your UI you'll have to either wrap it in a FutureBuilder or pass it to setState().
When a user logs into my flutter app, they have to log in, then they are brought to a screen with a feed of posts. I use a ListView.builder to take a list of posts from my database and create the feed of posts. My issue is that when the feed screen is initially launched, the ListView doesn't load. As soon as I hot-reload the app the list does load. I imagine there's a very obvious minor mistake in my code but I just can't find it. I will put all of the code from the feed screen below, please take a look and let me know if you see the mistake.
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
static const String id = "home_screen";
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
// List allposts = [(post: Post, owner: String)];
Color _likeButtonColor = Colors.black;
Widget _buildPost(String username, String imageUrl, String caption) {
return Container(
color: Colors.white,
child: Column(
children: [
Container(
height: 50,
color: Colors.deepOrangeAccent[100],
child: Row(
children: [
SizedBox(width: 5),
CircleAvatar(),
SizedBox(width: 5),
Text(username, style: TextStyle(fontSize: 15)),
SizedBox(width: 225),
Icon(Icons.more_horiz)
],
),
),
Stack(
children: [
Image.asset("images/post_background.jpg"),
Padding(
padding: const EdgeInsets.all(20.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(imageUrl, fit: BoxFit.cover)),
),
],
),
Container(
height: 100,
child: Column(
children: [
const SizedBox(height: 5),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: () {
setState(() {
HapticFeedback.lightImpact();
});
},
icon: Icon(Icons.thumb_up_alt_outlined, size: 30)),
Text("l", style: TextStyle(fontSize: 30)),
Icon(Icons.ios_share, size: 30)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(caption, style: const TextStyle(fontSize: 15))
],
)
],
),
)
],
),
);
}
List<Post> listPosts = [];
fetchPosts() async {
final userRef = FirebaseFirestore.instance.collection('users');
final QuerySnapshot result = await userRef.get();
result.docs.forEach((res) async {
print(res.id);
QuerySnapshot posts = await userRef.doc(res.id).collection("posts").get();
posts.docs.forEach((res) {
listPosts.add(Post.fromJson(res.data() as Map<String, dynamic>));
});
});
}
#override
void initState() {
fetchPosts();
print(listPosts);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: listPosts.length,
itemBuilder: (BuildContext context, int index) {
// We retrieve the post at index « index »
final post = listPosts[index];
// Replace with your actual implementation of _buildPost
return _buildPost(post.id, post.postUrlString, post.caption);
}),
);
}
}
The reason is that you need to rebuild your screen to show the reflected changes after performing an async operation (use setState to rebuild the UI). And secondly .forEach loop is not built to carry async stuff and are less efficient then a normal for loop so its better to change it.
fetchPosts() async {
final userRef = FirebaseFirestore.instance.collection('users');
final QuerySnapshot result = await userRef.get();
for(var res in result.docs)async{
print(res.id);
QuerySnapshot posts = await userRef.doc(res.id).collection("posts").get();
posts.docs.forEach((res) {
listPosts.add(Post.fromJson(res.data() as Map<String, dynamic>));
});
}
setState((){});//call it after end of your function
}
Ps:- You can use a variable named loading to show progress indicator and set it to false after fetching data in setState.
i'm trying to fetch Products collection from specific user, and the request isn't working.
here is my code:
the first request function:
Stream<QuerySnapshot<Object>> get productsUser {
return usersCollection.doc(uid).collection("Products").snapshots();
}
and here where I try to present the Products array I fetch (or didn't...):
class _ProductPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
final GivitUser givitUser = Provider.of<GivitUser>(context);
final DatabaseService db = DatabaseService(uid: givitUser.uid);
return StreamBuilder<QuerySnapshot>(
stream: db.productsUser,
builder: (context, snapshotProduct) {
if (snapshotProduct.hasError) {
return Text('Something went wrong');
}
if (snapshotProduct.connectionState == ConnectionState.waiting) {
return Loading();
}
return Container(
color: Colors.blue[100],
height: 400.0,
alignment: Alignment.topCenter,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: snapshotProduct.data.docs.map(
(DocumentSnapshot document) {
var snapshotdata = document.data() as Map;
Product product =
Product.productFromDocument(snapshotdata, document.id);
print(product.name);
return Container(
child: Text(product.name),
);
},
).toList(),
),
),
);
});
}
}
);
Thanks to everyone who will help! :)
You can either create a StatefulWidget, and store the result of the fetch as state, or you can use a StreamBuilder to manage the state for you, and automatically rebuild the widget tree each time a new snapshot is received. In either case, the following two guides may also be helpful:
Streams
Async/Await
Here's an example of how you might use StreamBuilder in your case:
Widget build(BuildContext context) {
return Container(
color: Colors.blue[100],
height: 400.0,
alignment: Alignment.topCenter,
child: SingleChildScrollView(
child: StreamBuilder<QuerySnapshot<Object>>(
stream: usersCollection.doc(uid).collection("Products").snapshots(),
builder: (context, asyncSnapshot) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: asyncSnapshot.data.data.docs.map(
(DocumentSnapshot document) {
var snapshotdata = document.data() as Map;
Product product =
Product.productFromDocument(snapshotdata, document.id);
print(product.name);
return Container(
child: Text(product.name),
);
},
).toList(),
),
),
),
);
}
There's a problem which I'm trying to solve, it is displaying data by recently added to Firestore, through Flutter. What can be done in my case?
In React I would achieve this with useState hook, how can this be achieved in Flutter?
I read about .sort(); method, is that a right way of doing this?
Code:
Form.dart
class FormText extends StatelessWidget {
final String _labelText = 'Enter your weight..';
final String _buttonText = 'Save';
final _controller = TextEditingController();
final dateFormat = new DateFormat.yMMMMd().add_jm();
final _collection =
FirebaseFirestore.instance.collection('weightMeasurement');
void saveItemToList() {
final weight = _controller.text;
if (weight.isNotEmpty) {
_collection.add({
'weight': weight,
'time': dateFormat.format(DateTime.now()),
});
} else {
return null;
}
_controller.clear();
}
#override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: TextField(
keyboardType: TextInputType.number,
controller: _controller,
decoration: InputDecoration(
labelText: _labelText,
),
),
),
FlatButton(
color: Colors.blue,
onPressed: saveItemToList,
child: Text(
_buttonText,
style: TextStyle(
color: Colors.white,
),
),
),
],
);
}
}
Measurements.dart
class RecentMeasurement {
Widget buildList(QuerySnapshot snapshot) {
return ListView.builder(
reverse: false,
itemCount: snapshot.docs.length,
itemBuilder: (context, index) {
final doc = snapshot.docs[index];
return Dismissible(
background: Container(color: Colors.red),
key: Key(doc.id),
onDismissed: (direction) {
FirebaseFirestore.instance
.collection('weightMeasurement')
.doc(doc.id)
.delete();
},
child: ListTile(
title: Expanded(
child: Card(
margin: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
Text('Current Weight: ' + doc['weight'] + 'kg'),
Text('Time added: ' + doc['time'].toString()),
],
),
),
),
),
);
},
);
}
}
Layout.dart
class Layout extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
FormText(),
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('weightMeasurement')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return Expanded(
child: RecentMeasurement().buildList(snapshot.data),
);
}),
],
),
);
}
}
You can try order by . Here is an example
firestoreDb.collection("weightMeasurement")
.orderBy("date", Query.Direction.ASCENDING)
You have to use "orderBy" on your collection, but previously You have to store something called timestamp. Make sure when You upload Your items to Firebase to also upload DateTime.now() along with Your items so You can order them by time. Do not forget to use Ascending direction since it will show you Your items ordered correctly.
I am trying to create a drop down list inside an alert dialog widget. The menu items need to be pulled from firebase. So far, I have created my alert dialog, looped through my firebase data and created a list from the results. The issue I am facing comes when I try to use my list as the "items" for my dropdown, when I run my code I get the following error:
type 'List<DropdownMenuItem<dynamic>>' is not a subtype of type 'List<DropdownMenuItem<String>>'
Here is my code:
class ViewSingleCard extends StatefulWidget {
final String imgUrl;
final String message;
ViewSingleCard({this.imgUrl, this.message});
#override
_ViewSingleCardState createState() => _ViewSingleCardState(imgUrl, message);
}
class _ViewSingleCardState extends State<ViewSingleCard> {
String imgUrl;
String message;
_ViewSingleCardState(this.imgUrl, this.message);
PageController _pageController = PageController(initialPage: 0);
int currentPage = 0;
#override
void dispose() {
super.dispose();
_pageController.dispose();
}
_onPageChanged(int index) {
setState(() {
currentPage = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurpleAccent,
title: Text('Viewer'),
actions: [
Stack(
children: [
IconButton(
icon: Icon(Icons.add),
onPressed: () {
createAlertDiaglog(context);
})
],
)
],
),
body: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
PageView(
scrollDirection: Axis.horizontal,
controller: _pageController,
onPageChanged: _onPageChanged,
children: [
Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Image(
image: FirebaseImage(imgUrl,
maxSizeBytes: 15 * 1024 * 1024))),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text(message),
),
),
],
),
Stack(
children: <Widget>[
Container(
margin: const EdgeInsets.only(bottom: 35),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
for (int i = 0; i <= 1; i++)
if (i == currentPage)
SlideDots(true)
else
SlideDots(false)
],
),
),
],
),
]),
);
}
createAlertDiaglog(BuildContext context) {
String selectedOccasion;
List<DropdownMenuItem> occasions = [];
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Add to collection"),
content: StreamBuilder<QuerySnapshot>(
stream: getCollectionInfo(context),
// ignore: missing_return
builder: (context, snapshot) {
if (!snapshot.hasData)
const Text("Loading.....");
else {
for (int i = 0; i < snapshot.data.docs.length; i++) {
DocumentSnapshot snap = snapshot.data.docs[i];
occasions.add(
DropdownMenuItem(
child: Text(
snap.id,
),
value: "${snap.id}",
),
);
}
}
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// ignore: missing_return
DropdownButton<String>(
items: occasions,
hint: Text("Style"),
value: selectedOccasion,
onChanged: (String Value) {
setState(() {
selectedOccasion = Value;
});
},
),
],
);
}),
);
});
}
Stream<QuerySnapshot> getCollectionInfo(BuildContext context) async* {
yield* FirebaseFirestore.instance
.collection('collections')
.doc(FirebaseAuth.instance.currentUser.uid)
.collection('occasions')
.snapshots();
}
}
Any help? Thanks
Here's the fix, add the <String> there:
occasions.add(
DropdownMenuItem<String>(
child: Text(
and also fix the type of the list (thanks to #nvoigt's answer)
List<DropdownMenuItem<String>> occasions = [];
Your DropDownButton is given the <String> type, so it's expecting the same thing from its items.
Whenever you get this exception, just swap the locations of the two types and think of an assignment. This means you are trying to do this kind of assignment
List<DropdownMenuItem<String>> a;
List<DropdownMenuItem<dynamic>> b;
a = b;
This:
List<DropdownMenuItem> occasions = [];
is a List<DropdownMenuItem<dynamic>>, but you want a List<DropdownMenuItem<String>>, so you need to make it one:
List<DropdownMenuItem<String>> occasions = [];
That said: you have an analyzer. Do not ignore it's warnings. You have ignored warnings that are correct, where you have made a mistake. Do not do this. Do not ignore your mistakes, fix them.