I have a collection called orders. In this I have a subcollection known as stockQuantity. Every day when new stock is added, the stock added that day along with the date are added as documents in the subcollection stockQuantity. I am trying to find the stock added on the latest date for each order and display it on a page known as StockPage, however nothing is being displayed on the page. The order must meet the criteria, orderStatus = 1.
An index has been created too.
This is my code for StockPage
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class StockPage extends StatefulWidget {
#override
State<StockPage> createState() => _StockPageState();
}
class _StockPageState extends State<StockPage> {
final Stream<QuerySnapshot> _stockStream = FirebaseFirestore.instance
.collection('orders')
.where("statusOrder", isEqualTo: 1)
.orderBy("stockQuantity.date", descending: true)
.snapshots();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Stock Quantity"),
),
body: StreamBuilder<QuerySnapshot>(
stream: _stockStream,
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> 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) {
var stockQuantity = snapshot.data!.docs[index]["stockQuantity"].last;
return ListTile(
title: Text(stockQuantity["quantity"].toString()),
subtitle: Text(stockQuantity["date"].toString()),
leading: Text('${snapshot.data!.docs.length}'),
);
},
);
}),
);
}
}
Nothing is being displayed on the page.
I tried using print statements to find the values of different variables, but none of them showed the stock quantity
Related
I am struggling to resolve these two errors.
error: The property 'docs' can't be unconditionally accessed because the receiver can be 'null'. (unchecked_use_of_nullable_value at [church_app] lib\Screens\DevotionalList.dart:23)
error: The parameter 'devotional' can't have a value of 'null' because of its type, but the implicit default value is 'null'. (missing_default_value_for_parameter at [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']),
),
],
);
}
}
Any assistance would be appreciated.
Tried adding null exceptions but I still run into the same error.
at the point when your call snapshot.data.docs, it can't be null since you checked first with the hasError, so you can tell this to Dart by explicitly adding a !, instead of this:
List<DocumentSnapshot> documents = snapshot.data.docs;
add !, so it will be this:
List<DocumentSnapshot> documents = snapshot.data.docs!;
this will fix both the error you're facing.
i am trying to call data from firestore but i have hit a problem. it keeps giving me this error even though every source of a solution i have seen use 'documents' to locate the particular data that is needed. help is needed very much
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class BoardApp extends StatefulWidget {
const BoardApp({Key? key}) : super(key: key);
#override
State<BoardApp> createState() => _BoardAppState();
}
class _BoardAppState extends State<BoardApp> {
var firestoreDb = FirebaseFirestore.instance.collection('board').snapshots();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('community board'),
),
body: StreamBuilder(
stream: firestoreDb,
builder: (BuildContext context, snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data?.documents.length,
itemBuilder: (context, int index){
return Text(snapshot.data?.documents[index]);
}
);
}else{
return CircularProgressIndicator();
}
}
),
);
i have done everything that is needed but i still get the error
You can use StreamBuilder<QuerySnapshot> to access the data from firestore by replacing StreamBuilder().
A QuerySnapshot is returned from a collection query, and allows you to inspect the collection, such as how many documents exist within it, and gives access to the documents within the collection.
You can refer the document & Cloud Firestore
This problem is about how to keep the favorited item in the List, even after I re-open the app.
I want to keep the favorited item in doaList into favDoa, even after I close my app and re-open it. I've seen about the shared_preference package in flutter to store data, but i confused how can i implement it into my app. Here is my code :
import 'package:flutter/material.dart';
import 'package:json_test/class/doa.dart';
import 'package:json_test/page/DoaPage.dart';
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
List<Doa> doaList;
List<Doa> favDoa;
bool _isInit = true;
Future<void> fetchDoa(BuildContext context) async {
final jsonstring =
await DefaultAssetBundle.of(context).loadString('assets/doa.json');
doaList = doaFromJson(jsonstring);
_isInit = false;
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("JSON Data test"),
),
body: Container(
child: FutureBuilder(
future: _isInit ? fetchDoa(context) : Future(null),
builder: (context, _) {
if (doaList.isNotEmpty) {
return ListView.builder(
itemCount: doaList.length,
itemBuilder: (BuildContext context, int index) {
Doa doa = doaList[index];
return Card(
margin: EdgeInsets.all(8),
child: ListTile(
title: Text(doa.judul),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
DoaPage(
doa: doa,
)));
},
trailing: IconButton(
icon: Icon(
doa.fav
? Icons.favorite
: Icons.favorite_border,
color: doa.fav ? Colors.red : null,
),
onPressed: () => setState(() {
doa.fav = !doa.fav;
}),
)));
},
);
}
return CircularProgressIndicator();
})));
}
}
when I click the favorite icon in the list, it will be marked true in the "doa.fav". How can I implement the shared_preference package in my code to keep the doa.fav's data? Thank you so much for your answer :)
Try this:
Create a list of integer to store the Doa Ids, say you name it favoriteList
Each time you click the favorite button, add the Doa's id to favoriteList. Also save it to shared_preferences. It only support list of string, so you need to convert it first, something like:
List<String> stringFavoriteIds =
favoriteList.map((e) => e.toString()).toList();
SharedPrefs().favoriteIds = stringFavoriteIds ;
Next, each time you open the app, load SharedPrefs().favoriteIds to favoriteList
Compare the Doa Ids in favoriteList to your list of doa to mark Doa.fav to true for matching Ids.
I am trying to get current signed in user data using future builder but this line {future: FirebaseAuth.instance.currentUser(),} throws an error{The expression doesn't evaluate to a function, so it can't be invoked.}. And removing brackets at the end of currentUser gives another error {The argument type 'User' can't be assigned to the parameter type 'Future'.}
//import 'dart:html';
import 'package:firebase_auth/firebase_auth.dart';
import './message_bubble.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class Messages extends StatelessWidget {
#override
Widget build(BuildContext context) {
return FutureBuilder(
//future:FirebaseAuth.instance.currentUser,
future: FirebaseAuth.instance.currentUser(),
//future: FirebaseAuth.instance.currentUser,
builder: (ctx, futureSnapshot) {
if (futureSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('chat')
.orderBy('createdAt', descending: true)
.snapshots(),
builder: (ctx, chatSnapshot) {
if (chatSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
final chatDocs = chatSnapshot.data.documents;
return ListView.builder(
reverse: true,
itemCount: chatDocs.length,
itemBuilder: (ctx, index) => MessageBubble(
chatDocs[index]['text'],
chatDocs[index]['userId'] == futureSnapshot.data.uid,
),
);
});
},
);
}
}
```[error is present in line number 14 ,screenshots of error is also attached. I also tried by removing brackets at the end then another error arises(The argument type 'User' can't be assigned to the parameter type 'Future<dynamic>'.)[\]\[1\]][1]
[1]: https://i.stack.imgur.com/4x2JI.jpg
You don't have to user FutureBuilder to get your User. currentUser is not a Future anymore but just a getter.
User user = FirebaseAuth.instance.currentUser;
I need one-time read Data from Firebase Cloud, thats why I use FutureBuilder in my project (dart/flutter). But when the application is started it reads without stopping (as stream). What should I do to fix this?
class Hello extends StatefulWidget {
#override
_HelloState createState() => _HelloState();
}
class _HelloState extends State<Hello> {
Future getPosts() async{
QuerySnapshot qn = await FirebaseFirestore.instance.collection("111").get();
return qn.docs;
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: new Text('Hello'),
),
body: FutureBuilder(
future: getPosts(),
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: CircularProgressIndicator(),
);
}
else{
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return Text(snapshot.data[index].data()["subject"]);
},
);
}
},
),
);
}
}
From the FutureBuilder doc :
The future must have been obtained earlier, e.g. during
State.initState, State.didUpdateConfig, or
State.didChangeDependencies. It must not be created during the
State.build or StatelessWidget.build method call when constructing the
FutureBuilder. If the future is created at the same time as the
FutureBuilder, then every time the FutureBuilder's parent is rebuilt,
the asynchronous task will be restarted.
Example :
Future<QuerySnapshot> future;
#override
void initState() {
super.initState();
future = Firestore.instance.collection("111").getDocuments();
}
// future : future