Flutter: Connection closed while receiving data
I experienced this problem today (5th April 2022) while working with an external API for a mobile application and I would like to leave this here as I didn`t find a conclusive answer for the issue to my satisfaction.
Context: The Mobile Application is on Flutter 2.10
The API is doing the usual future async await. Its a GET request. Fetching data from a Spring Boot Java Server.
My initial thought was a keep alive header was missing. Adding it didn`t fix the issue.
A query through Insomnia (I find POSTMAN a bit heavy especially when am doing both backend and frontend on the same device) showed a non expected issue.
The issue is a Race Condition as a result of fetching models that “call on each other”. Let me explain. We have a model Teacher and a Student. Teacher is a parent model, and Student a child model. So in normal correct programming, Its
Teacher — Student
But in mine I was referencing the Teacher on the student model also causing a cyclic event. A loop. i.e. Teacher — Student-teacher-student…. scenario. By removing the Student teacher relationship and maintaining the teacher student relationship I was able to solve by extension the Flutter bug.
If your not having a cyclic problem, your probably fetching too much collection of data. Consider implementing a pagination to reduce the amount of data fetched per each instance.
Happy Coding….