I use the GetX package in my flutter app. However, I’ve noticed that when it comes to updating the UI with new data especially when navigation certain pages, the previous data in the model shows for a few seconds before the new data gets updated. Is this some poor implementation or anissue with GetX.
My issue is with the _workoutDataModel
. If i have previously visited a workout and prees the back button, wanting to view another workout, the previous workout still shows for a few seconds before updating to the new data, sometimes up to 5 seconds.
import 'dart:async';
import 'package:fit/models/workout/exercise_model.dart';
import 'package:fit/models/workout/workout_data_model.dart';
import 'package:fit/models/workout/workout_list_model.dart';
import 'package:fit/repositories/workout/workout_repository.dart';
import 'package:get/get.dart';
class WorkoutController extends GetxController {
final WorkoutRepository workoutRepository;
WorkoutController({required this.workoutRepository});
late WorkoutDataModel _workoutDataModel;
WorkoutDataModel get workoutDataModel => _workoutDataModel;
bool _isWorkoutData = false;
bool get isWorkoutData => _isWorkoutData;
Future<void> getWorkoutData(String url) async {
Response response = await workoutRepository.getWorkoutData(url);
update();
if (response.statusCode == 200) {
_workoutDataModel = WorkoutDataModel.fromJson(response.body);
_isWorkoutData = true;
update();
} else {}
}
}
3
Answers
I suspect it is because you are using update twice, you could limit to only one.
if you are using getx then you must properly apply stateful variables, I gave two examples with type WorkoutDataModel and bool
it would also be correct to move the marked part of the code to your provider file
Verify in logs that when you press back your controller is deleted. As you wrote, data is visible on revisit means controller is not getting deleted.
Now, Solution:
WorkoutDataModel
with empty constructor, which in turn will display you desired result.