I am building a Flutter application that loads a document
from Cloud Firestore and displays its content in a BottomSheet
widget.
The read is performed on a collection
(named details) that has almost 4K documents
in it. Also, each document
has a complex JSON, where the requested data is. This JSON has 2 simple Map
objects, one array
and up 10 Strings
.
I want to read a single document
from details for displaying it on the BottomSheet
, so I request the desired document
with an specific given id
in initState()
, upon the creation of the BottomSheet
.
// CustomBottomSheet Widget
CustomData info;
@override
void initState() {
_getDocument();
super.initState();
}
Future<void> _getDocument() async {
final FirebaseFirestore _fireStoreReference = FirebaseFirestore.instance;
try {
await _fireStoreReference.collection("details").doc(widget.uid).get().then((snapshot) {
if (snapshot != null && snapshot.exists) {
setState(() {
// fromJson is a factory method from CustomData to translate
// the Map<String, dynamic> from Firebase into a
// CustomData Object
info = CustomData.fromJson(snapshot.data());
});
}
});
} catch (err) {
//...
}
}
@override
Widget build(BuildContext context) {
return BottomSheet(
onClosing: () => {},
builder: (context) {
return Container(
child: // Display data of CustomData
)
}
);
}
// CustomBottomSheet Widget is called from the Main Page as so
...
GestureDetector(
onTap: () async {
await showModalBottomSheet(context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
isDismissible: false,
builder: (context) {
return CustomBottomSheet(uid: "0001");
}
).then((_) {
// Uploads some UI when BottomSheet is closed
});
},
child: Container(width: 150, height: 90, color: Colors.orange, child: Text("Show Data"))
),
...
This query should cost only 1 READ
operation, but whenever I show the BottomSheet
, Cloud Firestore charges me with +7K READ
operations. This morning I went from having 619 reads to 8.2K reads only after requesting a single document
of "details".
My thoughts were that I was somehow requesting it that many times or perhaps that the JSON inside each document
is so complex and large that it costs exactly that amount of READ
operations each time I request it. With these things in mind, I searched for answers, but everywhere I read says that reading one document
, no matter what is inside, it will cost 1 READ
.
I also debugged the application and, as expected, initState()
is only called once and therefore, the document
is read only once on execution, so I do not know from where these insane amounts of READ
operations come from.
Furthermore, it came into my mind that just looking up the 4K documents
inside details on the Firebase Console maybe is correlated to the problem, as you are requesting the reading of those documents as you scroll by them. But I do not know.
question from:
https://stackoverflow.com/questions/65873979/why-are-there-so-many-read-operations-with-a-function-that-loads-a-single-docume