setState() method in Flutter is not updating the view until the last call in a for loop
Try using Future.delay like
void _bubbleSortVisualiser() async { print('Bubble sort function called'); List<int> bubbleArr = List.from(arr); for (int i = 0; i < bubbleArr.length - 1; i++) { for (int j = 0; j < bubbleArr.length - 1 - i; j++) { int temp; if (bubbleArr[j] < bubbleArr[j + 1]) { temp = bubbleArr[j]; bubbleArr[j] = bubbleArr[j + 1]; bubbleArr[j + 1] = temp; //Every time arr changes setState() is called to visualise the changing array. await Future.delayed(const Duration(seconds: 1), () { setState(() { arr = List.from(bubbleArr); print("Updated to : $arr"); }); }); } } } }}
This line:
class HomePageState extends State<StatefulWidget> {
should be
class HomePageState extends State<HomePage> {
That's all. It should work then.
Also, you should change this:
@override State<StatefulWidget> createState() { return HomePageState(); }
to:
@override State<HomePage> createState() => HomePageState();