How can I retrieve a data from firebase database with Timer function in flutter app?
P.S.After user request data will be retrieved from firebase database 5 minutes later.
Thank you
Future.delayed(Duration(minutes: 5), () {
Timer.periodic(Duration(minutes: 5), (Timer t) {
setState(() {});
});
});
okay put this in the stateful widget with you StreamBuilder
this worked on my emulator. Let me know if it works for you.
My code is just like below but it did not work with your code.@wcyankees424
body: StreamBuilder(
stream: qs.collection('collection').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text('Loading');
return Center(
child: CircularProgressIndicator(),
);
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
List<DocumentSnapshot> listedQS = snapshot.data.documents;
var random = new Random();
for (var i = listedQS.length - 1; i > 0; i--) {
var n = random.nextInt(i + 1);
var temp = listedQS[i];
listedQS[i] = listedQS[n];
listedQS[n] = temp;
}
DocumentSnapshot mypost = listedQS[0];
Future<void> addEntry(Brew newBrew) async {
//value you want saved are stored in newBrew and passed in
Map<String, Object> entryData = {
'name': newBrew.name,
'strength': newBrew.strength,
'sugars': newBrew.sugars,
'index': newBrew.index,
};
await Firestore.instance.collection('//collection name').add(entryData);
}
Future<Brew> getEntries(Brew newBrew) async {
QuerySnapshot snapshot = await Firestore.instance
.collection('//Collection name')
.where('index', isGreaterThanOrEqualTo: Random().nextInt('//this number should be higher than the number of documents'))
.orderBy('index')
.limit(1)
.getDocuments();
if (snapshot.documents.isNotEmpty) {
Map<String, dynamic> documentData = snapshot.documents[0].data;
return Brew(
strength: documentData['strngth'],
sugars: documentData['sugars'],
name: documentData['name'],
index: documentData['index'],
);
} else {
snapshot = await Firestore.instance
.collection('//Collection name')
.where('index', isGreaterThanOrEqualTo: 0)
.orderBy('index')
.limit(1)
.getDocuments();
Map<String, dynamic> documentData = snapshot.documents[0].data;
return Brew(
strength: documentData['strngth'],
sugars: documentData['sugars'],
name: documentData['name'],
index: documentData['index'],
);
}
}
class Brew {
final String name;
final String sugars;
final int strength;
final int index;
Brew({
this.name,
this.sugars,
this.strength,
this.index,
});
}
you would create and field for every called index which would start at 0 increment by 1 for every entry in your in your database. This might help you
My code is like below how could I show text and image values with Querysnapshot method ? That you've mentioned with brew code! I cant use your code with below code structure...
Your code: @wcyankees424
snapshot = await Firestore.instance
.collection('//Collection name')
.where('index', isGreaterThanOrEqualTo: 0)
.orderBy('index')
.limit(1)
.getDocuments();
my code:
body: StreamBuilder(
stream: qs.collection('collection').limit(1).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text('Loading');
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
List<DocumentSnapshot> listedQS =
snapshot.data.documents; //listed documents
var random = new Random(); //dart math
for (var i = listedQS.length - 1; i > 0; i--) {
var n = random.nextInt(i + 1);
var temp = listedQS[i];
listedQS[i] = listedQS[n];
listedQS[n] = temp;
}
DocumentSnapshot mypost = listedQS[0];
return Stack(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: 350,
child: Padding(
padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
child: Material(
color: Colors.white,
elevation: 14.0,
shadowColor: Color(0x882196F3),
child: Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
Container(
width:
MediaQuery.of(context).size.width,
height: 200,
child: Image.network(
'${mypost['image']}',
fit: BoxFit.fill),
),
SizedBox(height: 10.0),
Text(
'${mypost['title']}',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 10.0),
Text(
'${mypost['subtitle']}',
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.blueGrey),
),
],
),
),
),
),
),
),
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: Firestore.instance.collection('fortunepool').where('index', isGreaterThanOrEqualTo: 0).orderBy('index').limit(1).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container(child: CircularProgressIndicator());
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
List<DocumentSnapshot> listedQS =
snapshot.data.documents; //listed documents
var random = new Random(); //dart math
for (var i = listedQS.length - 1; i > 0; i--) {
var n = random.nextInt(i + 1);
var temp = listedQS[i];
listedQS[i] = listedQS[n];
listedQS[n] = temp;
}
DocumentSnapshot mypost = listedQS[0];
return Stack(
So what you are do by calling
where('index' isEqualTo: ' 0'
is it first finds any documents with a field named index like above then it only returns the ones the has a value this is equal to to the one you provided
My code is just like below. I aso updated database as you've indicated above but still white screen
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: Firestore.instance
.collection('fortunepool')
.where('index', isGreaterThanOrEqualTo: 0)
.orderBy('index')
.limit(1)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container(child: CircularProgressIndicator());
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
List<DocumentSnapshot> listedQS =
snapshot.data.documents; //listed documents
DocumentSnapshot mypost = listedQS[0];
return Stack(