Method not returning expected result flutter - firebase

I'm working on this activity-chart using firebase and I wanted to implement the logic for retrieving the data from Firebase. To get the data I wrote the following method:
static Future<double> spotData(String day, queryDocumentSnapshot) async {
final documentSnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid.toString())
.collection('activity')
.doc('${globals.year}')
.collection('week${globals.week}')
.doc(day.toString())
.get();
if (documentSnapshot.exists) {
print('Document exists');
return documentSnapshot['usage'].toDouble();
} else {
print('Document doesnt exist');
return 0;
}
// return 0; // you don't need this anymore
}
I thought it should work but turns out 0 is returned... I think that's because of the "return 0;" at the end of the method... I tried removing it but I can't since I the method could return null and flutter null safety, rightfully so, prevents that from happening... In my Console I get the Output "Document exists" - that's why I believe, that the basic if-statement logic works but I need some help with the return statement...
I'm very thankful for all kinds of help:)
class SomellierChart extends StatefulWidget {
SomellierChart(
{Key? key,
required this.color,
required this.textColor,
required this.queryDocumentSnapshot})
: super(key: key);
Color textColor;
Color color;
List<QueryDocumentSnapshot> queryDocumentSnapshot;
#override
_SomellierChartState createState() => _SomellierChartState();
}
class _SomellierChartState extends State<SomellierChart> {
static Future<double> spotData(String day, queryDocumentSnapshot) async {
final documentSnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid.toString())
.collection('activity')
.doc('${globals.year}')
.collection('week${globals.week}')
.doc(day.toString())
.get();
if (documentSnapshot.exists) {
print('Document exists');
return documentSnapshot['usage'].toDouble();
} else {
print('Document doesnt exist');
return 0;
}
}
#override
Widget build(BuildContext context) {
return LineChart(
LineChartData(
gridData: FlGridData(
show: false,
),
titlesData: FlTitlesData(
show: true,
rightTitles: SideTitles(showTitles: false),
topTitles: SideTitles(showTitles: false),
leftTitles: SideTitles(showTitles: false),
bottomTitles: SideTitles(
showTitles: true,
reservedSize: 20,
interval: 1,
getTextStyles: (context, value) => GoogleFonts.poppins(
textStyle: TextStyle(
color: Theme.of(context).indicatorColor,
fontWeight: FontWeight.w600,
fontSize: 12,
),
),
getTitles: (value) {
switch (value.toInt()) {
case 0:
return 'Mon';
case 2:
return 'Tue';
case 4:
return 'Wed';
case 6:
return 'Thu';
case 8:
return 'Fri';
case 10:
return 'Sat';
case 12:
return 'Sun';
}
return '';
},
margin: 5,
),
),
borderData: FlBorderData(show: false),
minX: 0,
maxX: 12,
minY: 0,
maxY: 3,
lineBarsData: [
LineChartBarData(
spots: [
FlSpot(0, spotData('1', widget.queryDocumentSnapshot)),
FlSpot(2, spotData('2', widget.queryDocumentSnapshot)),
FlSpot(4, spotData('3', widget.queryDocumentSnapshot)),
FlSpot(6, spotData('4', widget.queryDocumentSnapshot)),
FlSpot(8, spotData('5', widget.queryDocumentSnapshot)),
FlSpot(10, spotData('6', widget.queryDocumentSnapshot)),
FlSpot(12, spotData('7', widget.queryDocumentSnapshot)),
],
isCurved: true,
curveSmoothness: 0.5,
preventCurveOverShooting: true,
colors: [widget.textColor],
barWidth: 3,
isStrokeCapRound: true,
dotData: FlDotData(
show: false,
),
belowBarData: BarAreaData(
show: true,
colors: [
widget.color.withOpacity(0.4),
],
),
),
],
),
);
}
}
this is the code im currently using... I made it stateful because I thought maybe I could update the UI that way but I dont believe that's necessary... How should I implement the FutureBuilder?

You are making a request to firebase, all requests to firebase are asynchronous, meaning they might be processed immediately, or after a while or at some point or another, which is why you are using the .then.
So basically, your function runs, gets to the asynchronous calculation, continues, reaches the return 0. returns, and only then, the .then method calls itself and you get to work with the result, by that time, your function is long over and it already returned 0, because your original function is synchronous.
To fix this, you must make your original function asynchronous, and use the await keyword:
static Future<double> spotData(String day, queryDocumentSnapshot) async {
final documentSnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid.toString())
.collection('activity')
.doc('${globals.year}')
.collection('week${globals.week}')
.doc(day.toString())
.get();
if (documentSnapshot.exists) {
print('Document exists');
return documentSnapshot['usage'].toDouble();
} else {
print('Document doesnt exist');
return 0;
}
// return 0; // you don't need this anymore
}
The problem of course it that you now return a future instead of the double, to use the future on UI you probably will need a FutureBuilder

Related

Data is not being read from firebase Realtime Database using a Stream in Flutter

since Firebase was updated I have been having some issues, first it was because now, instead of a map, an Object? is returned and that is now fixed, but I can't seem to get any data from the database. I can put it there fine but the reading is not working.
This is on my firebase_utils .dart file
FirebaseDatabase data = FirebaseDatabase.instance;
DatabaseReference database = data.ref();
Future<void> init() async {
FirebaseAuth.instance.userChanges().listen((user) {
if (user != null) {
//_loginState = ApplicationLoginState.loggedIn;
} else {
//_loginState = ApplicationLoginState.loggedOut;
}
});
if (!kIsWeb) {
data.setPersistenceEnabled(true);
data.setPersistenceCacheSizeBytes(10000000);
}
}
I have this class:
class ReservationStreamPublisher {
Stream<List<Reservation>> getReservationStream() {
final stream = database.child('reservations').onValue;
final streamToPublish = stream
.map((event) {
List<Reservation> reservationList = [];
Map<String, dynamic>.from(event.snapshot.value as dynamic)
.forEach((key, value) => reservationList.add(Reservation.fromRTDB(value)));
print(reservationList);
return reservationList;
});
return streamToPublish;
}
}
Next is my Reservation.fromRTDB
factory Reservation.fromRTDB(Map<String, dynamic> data) {
return Reservation(
pin: data['pin'],
day: data['day'],
hour: data['hour'],
duration: data['duration'],
state: data['state'],
userEmail: data['client_email'],
completed: data['completed'],
id: data['id'],
dateMade: '',
timeMade: '');
}
And this is one of the places where I am supposed to show data
Text('Slots Reservados neste dia:'),
_selectedDate != null
? StreamBuilder(
stream:
ReservationStreamPublisher().getReservationStream(),
builder: (context, snapshot) {
final tilesList = <ListTile>[];
if (snapshot.hasData) {
List reservations =
snapshot.data as List<Reservation>;
int i = 0;
do {
if (reservations.isNotEmpty) {
if (reservations[i].day !=
(DateFormat('dd/MM/yyyy')
.format(_selectedDate!))) {
reservations.removeAt(i);
i = i;
} else
i++;
}
} while (i < reservations.length);
try {
tilesList
.addAll(reservations.map((nextReservation) {
return ListTile(
leading: Icon(Icons.lock_clock),
title: Text(
"Das ${nextReservation.hour} as ${nextReservation.duration}"),
);
}));
} catch (e) {
return Text(
'Ainda nao existem reservas neste dia');
}
}
// }
if (tilesList.isNotEmpty) {
return Expanded(
child: ListView(
children: tilesList,
),
);
}
return Text('Ainda nao existem reservas neste dia');
})
: SizedBox(),
I am not getting any error at the moment, but no data is returned.This is a Reservation example on my RTDB

Flutter: How to remove awaits to take advantage of Firebase Offline Persistence?

I'm using a drop-down list (DropDown), whose elements are obtained from Firebase. The form works right, however when the internet connection is lost the Firebase Offline Persistence property doesn't work and the CircularProgressIndicator stays active. Reading some responses such as Using Offline Persistence in Firestore in a Flutter App, it is indicated that awaits should not be handled, however it is not clear to me how to achieve it:
class EstanqueAlimentarPage extends StatefulWidget {
#override
_EstanqueAlimentarPageState createState() => _EstanqueAlimentarPageState();
}
class _EstanqueAlimentarPageState extends State<EstanqueAlimentarPage> {
final formKey = GlobalKey<FormState>();
AlimentoBloc alimentoBloc = new AlimentoBloc();
AlimentoModel _alimento = new AlimentoModel();
AlimentarModel alimentar = new AlimentarModel();
List<AlimentoModel> _alimentoList;
bool _alimentoDisponible = true;
#override
void dispose() {
alimentoBloc.dispose();
super.dispose();
}
#override
void initState() {
_obtenerListaAlimentoUnaVez();
super.initState();
}
Future<void> _obtenerListaAlimentoUnaVez() async {
_alimentoList = await alimentoBloc.cargarAlimento(idEmpresa); // Await that I want to eliminate
if (_alimentoList.length > 0) { // Here appears a BAD STATE error when the internet connection goes from off to on
_alimento = _alimentoList[0];
_alimentoDisponible = true;
} else {
_alimentoDisponible = false;
}
_cargando = false;
setState(() {});
}
#override
Widget build(BuildContext context) {
return Form(
key: formKey,
child: Column(
children: <Widget> [
_crearTipoAlimento(_alimentoList),
SizedBox(height: 8.0),
_crearComentarios(),
]
)
),
_crearBoton('Guardar'),
}
Widget _crearTipoAlimento(List<AlimentoModel> lista) {
return Container(
decoration: _cajaBlanca,
child:
!_cargando // If it isn't loading, Dropdown must be displayed
? DropdownButtonFormField<AlimentoModel>(
decoration: InputDecoration(
labelText: 'Nombre del Alimento',
contentPadding: EdgeInsets.only(top:5.0),
prefixIcon: Icon(FontAwesomeIcons.boxOpen, color: Theme.of(context).primaryColor,),
border: InputBorder.none,
),
value: _alimento,
items: lista.map((AlimentoModel value) {
return DropdownMenuItem<AlimentoModel>(
child: Text(value.nombre),
value: value,
);
}).toList(),
onChanged: (_alimentoDisponible) ? (AlimentoModel _alimentoSeleccionado) {
print(_alimentoSeleccionado.nombre);
_alimento = _alimentoSeleccionado;
setState(() {});
} : null,
disabledHint: Text('No hay Alimento en Bodega'),
onSaved: (value) {
alimentar.idAlimento = _alimento.idAlimento;
alimentar.nombreAlimento = _alimento.nombreRef;
}
)
: Center (child: CircularProgressIndicator(strokeWidth: 1.0,))
);
}
Widget _crearComentarios() {
return TextFormField(
// -- DESIGN OTHER FIELDS -- //
onSaved: (value) {
alimentar.comentarios = value;
}
),
);
}
Widget _crearBoton(String texto) {
return RaisedButton(
// -- DESIGN -- //
onPressed: (_guardando) ? null : _submit,
),
);
}
void _submit() {
// CODE TO WRITE FORM IN FIREBASE
}
}
The function code from my BLOC is:
Future<List<AlimentoModel>> cargarAlimento(String idEmpresa, [String filtro]) async {
final alimento = await _alimentoProvider.cargarAlimento(idEmpresa, filtro); //It's one await more
_alimentoController.sink.add(alimento);
return alimento;
}
And the Query from PROVIDER is:
Future<List<AlimentoModel>> cargarAlimento(String idEmpresa, [String filtro]) async {
Query resp;
final List<AlimentoModel> alimento = new List();
resp = db.child('empresas').child(idEmpresa).child('bodega/1').child('alimento')
.orderByChild('cantidad').startAt(0.000001);
return resp.once().then((snapshot) {
if (snapshot.value == null) return [];
if (snapshot.value['error'] != null) return [];
snapshot.value.forEach((id, alim){
final temp = AlimentoModel.fromJson(Map<String,dynamic>.from(alim));
temp.idAlimento = id;
alimento.add(temp);
});
return alimento;
});
When using Firebase offline, you omit the await only on things that change the server (e.g., creating or updating a record). So you won't wait for the server to say "yes I wrote it", you assume that it's written.
In your case, however, you are not writing data, you are reading data. You will have to keep await in your example. The way you load your data has orderByChild and startAt, maybe those are preventing offline loading. Normally, you get it if it's already in the cache: https://firebase.google.com/docs/firestore/manage-data/enable-offline#get_offline_data
You mention a BAD STATE error, maybe if you provide that, we may be able to pinpoint the issue a bit better.

Flutter - StreamProvider - Sending reoccuring XHR Requests to Firebase

I noticed my read requests increased significantly since I added a new Stream using StreamProvider, I managed to confirm this by removing the stream and no repeating requests where made. But I can't seem to track down why its repeating.
Note: The code functions fine, just these read requests have gone through the roof.
Stream
//get list of all open enquiries
Stream<List<EnquiryData>> get centreDashboardOpenEnquiries {
return centresCollection.document(centreID).collection('enquiries').where('enquiryStatus', whereIn: ['New', 'Contacted', 'Tour Scheduled', 'Tour Completed']).snapshots().map(_enquiryList);
}
//Map QuerySnapshot to List of EnquiryData
List<EnquiryData> _enquiryList(QuerySnapshot enquiry){
return enquiry.documents.map((doc) {
return EnquiryData(
enquiryID: doc.data['enquiryID'].toString(),
parentFirstName: doc.data['firstName'],
parentLastName: doc.data['lastName'],
parentPhoneNumber: doc.data['phoneNumber'],
parentEmail: doc.data['email'],
parentAddress: doc.data['address'],
methodOfEnquiry: doc.data['methodOfEnquiry'],
hearAboutUs: doc.data['hearAboutUs'],
specificHearAboutUs: doc.data['specificHearAboutUs'],
impressionOfCentre: doc.data['firstImpression'],
enquiryStatus: doc.data['enquiryStatus'],
created: doc.data['created'],
lastUpdated: doc.data['lastUpdated'],
);
}).toList();
}
Call Stream with CurrentOpenDataTable child
child: StreamProvider<List<EnquiryData>>.value(
value: EnquiryDatabaseService(centreID: widget.centreData.centreID).centreDashboardOpenEnquiries,
child: CurrentOpenDataTable(),
),
CurrentOpenDataTable()
class CurrentOpenDataTable extends StatefulWidget {
#override
_CurrentOpenDataTableState createState() => _CurrentOpenDataTableState();
}
class _CurrentOpenDataTableState extends State<CurrentOpenDataTable> {
#override
Widget build(BuildContext context) {
final enquiryData = Provider.of<List<EnquiryData>>(context) ?? [];
return DataTable(
showCheckboxColumn: false,
sortColumnIndex: 1,
sortAscending: true,
columns: [
DataColumn(
label: Text('Date'),
),
DataColumn(
label: Text('Name'),
//numeric: true,
),
DataColumn(
label: Text('Status'),
),
],
rows: _enquiryRow(context, enquiryData),
);
}
}
_dateFormat(DateTime dateTime) {
String formattedDate = DateFormat('dd/MM/yyyy').format(dateTime);
return formattedDate;
}
_enquiryRow(BuildContext context, List<EnquiryData> enquiryData) {
List<DataRow> listOfDataRows = List.generate(
enquiryData.length,
(index) => DataRow(
cells: [
DataCell(Text(_dateFormat(DateTime.fromMillisecondsSinceEpoch(
enquiryData[index].created)))),
DataCell(Text(enquiryData[index].parentFirstName +
' ' +
enquiryData[index].parentLastName)),
DataCell(Text(enquiryData[index].enquiryStatus)),
],
onSelectChanged: (bool selected) {
if (selected) {
showEnquiryDialog(context, enquiryData[index]);
}
}));
return listOfDataRows;
}
The widget (let's call it MyWidget) that contains the StreamProvider is constantly being rebuilt.
You shouldn't be creating the EnquiryDatabaseService inside StreamProvider<List<EnquiryData>>.value, instead, get the stream inside initState.
Example
class _MyWidgetState extends State<MyWidget> {
Stream<List<EnquiryData>> _stream;
#override
initState() {
_stream = EnquiryDatabaseService(centreID: widget.centreData.centreID). centreDashboardOpenEnquiries;
}
// ... in build
StreamProvider.value(
value: _stream,
child: Container(/* ... */),
),
}
Or don't use the .value constructor.
StreamProvider(
create: (_) => EnquiryDatabaseService(centreID: widget.centreData.centreID). centreDashboardOpenEnquiries,
child: Container(/* ... */),
),
Read more on the do's and don'ts here
More reference here - How to deal with unwanted widget build?

How to convert Future<int> to int?

So I'm trying to display a pie chart using the fl_chart plugin. The data for the chart is being retrieved from firestore. I have this function that is used to display the data:
List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) {
return List.generate(length, (i) {
final isTouched = i == touchedIndex;
final double fontSize = isTouched ? 25 : 16;
final double radius = isTouched ? 60 : 50;
return PieChartSectionData(
color: Color(int.parse(cerealData[i].colorVal)),
value: cerealData[i].rating,
title: cerealData[i].rating.toString(),
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
});
}
The List.generate() takes an int as an argument. Since I'm displaying realtime data, I'm trying to get the number of documents present in my collection. For that, I have a function called getLength():
void getLength(AsyncSnapshot<QuerySnapshot> snapshot) async {
length = await Firestore.instance.collection('cereal').snapshots().length;
cerealData =
snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
}
However, when I run the code, I get:
Another exception was thrown: type 'Future<int>' is not a subtype of type 'int'
The entire code:
class _FlChartPageState extends State<FlChartPage> {
int touchedIndex;
var length;
List<Cereal> cerealData;
void getLength(AsyncSnapshot<QuerySnapshot> snapshot) async {
length = await Firestore.instance.collection('cereal').snapshots().length;
cerealData =
snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('cereal').snapshots(),
builder: (context, snapshot) {
getLength(snapshot);
if (!snapshot.hasData)
return CircularProgressIndicator();
else {
return PieChart(PieChartData(
pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) {
setState(() {
if (pieTouchResponse.touchInput is FlLongPressEnd ||
pieTouchResponse.touchInput is FlPanEnd) {
touchedIndex = -1;
} else {
touchedIndex = pieTouchResponse.touchedSectionIndex;
}
});
}),
borderData: FlBorderData(show: false),
sectionsSpace: 0,
centerSpaceRadius: 40,
sections: showSection(snapshot)));
}
}),
);
}
List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) {
return List.generate(length, (i) {
final isTouched = i == touchedIndex;
final double fontSize = isTouched ? 25 : 16;
final double radius = isTouched ? 60 : 50;
return PieChartSectionData(
color: Color(int.parse(cerealData[i].colorVal)),
value: cerealData[i].rating,
title: cerealData[i].rating.toString(),
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
});
}
}
I read somewhere that awaiting the future gets rid of the Future. But that doesn't work here.
How do I fix this?
Edit: It works if I simply pass the number of documents instead of length in List.generate(). But this won't work if there are changes to the collection. So how do I convert Future to int?
I think you aren't getting the length of the documents, you are getting the length of the snapshots if you want to get the documents length :
QuerySnapshot querySnapshot = await Firestore.instance.collection('cereal').getDocuments();
int length = querySnapshot.documents.length;
In get getLength function you are trying to get length which is actually async task which returns future and because of that you are getting following error.
Change your method with following metod
getLength()async{
Firestore.instance.collection('cereal').snapshots().length.then((len){
length = len;
cerealData =
snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
});
}

How to get data from Firestore in Scoped Model - Flutter

I'm trying to get data from Firestore, in debug print the future does it job and list gets data and in debugPrint length is +, but when I try to get data in another Widget list recives null, in debugPrint length is 0 .
model.dart
class BBModel extends Model {
int _counter = 10;
int get counter => _counter;
var db = dbBB;
List<BB> _bbs;
List<BB> get bbs => _bbs;
Future<List<BB>> getBBs() async {
var snapshot = await db.getDocuments();
for (int i = 0; i < snapshot.documents.length; i++) {
_bbs.add(BB.fromSnapshot(snapshot.documents[i]));
print(bbs.length.toString()); //recives 23
}
notifyListeners();
return _bbs;
}
}
main.dart
void main() {
var model = BBModel();
model.getBBs();
runApp(ScopedModel<BBModel>(model: BBModel(), child: MyApp()));
}
statefullpage.dart
Expanded(
flex: 1,
child: Container(
height: 400.0,
child: ScopedModelDescendant<BBModel>(
builder: (context, child, model) {
return ListView.builder(
itemCount: model.bbs.length,
itemBuilder: (context, index) {
return Text(model.bbs[index].bbID);
});
}))),
Looks like the code you're written in main.dart is wrong. The instatiated model is different from the one you've sent in your ScopedModel.
Correction
Change model: model to model: BBModel() in your main.dart file.
void main() {
final model = BBModel();
model.getBBs();
runApp(ScopedModel<BBModel>(model: model, child: MyApp()));
}
In main.dart, I would try doing:
void main() {
var model = BBModel();
model.getBBs().then((someVariableName){
runApp(ScopedModel<BBModel>(model: BBModel(), child: MyApp()));
});
}
note: "someVariableName" will contain a List< BB>
To wait you can use the
await model.getBBs();
Apart from this however, I do not recommend uploading data to the main, as you would slow down the use of the app, as the data is getting bigger. Upload the data only to the pages you need and find a way to do this.

Resources