What occurs when a brand new occasion of a widget has identical key because the outdated occasion and the childrens have totally different state within the new widget occasion?
Within the Flutter docs, it says that The brand new Widget is taken into account to correspond to an present Aspect if it has the identical Sort and Key.
Hyperlink to the doc
For instance within the code under
Widget loadingStudent(Record absenteesList, int studentId) {
return ListView.separated(
key: someConstGlobalKey,
addAutomaticKeepAlives: true,
separatorBuilder: (context, index) {
return Divider();
},
itemCount: studentsList.size,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return StudentCard(
studentObject: studentsList[index],
classAttendanceDetails: classAttendanceDetails,
isLocked: widget.isLocked,
course: course,
studentId: studentsList[index].id,
index: index,
studentsList: studentsList,
absenteeList: absenteesList,
isAbsent: isAbsent(studentsList[index].id, absenteesList),
currentlyMarkedStudentId: studentId,
isLoading: true,
);
});
}
Widget loadedStudents(Record absenteesList) {
return ListView.separated(
key: someConstGlobalKey,
separatorBuilder: (context, index) {
return Divider();
},
itemCount: studentsList.size,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return StudentCard(
studentObject: studentsList[index],
classAttendanceDetails: classAttendanceDetails,
isLocked: widget.isLocked,
course: course,
studentId: studentsList[index].id,
index: index,
studentsList: studentsList,
absenteeList: absenteesList,
isAbsent: isAbsent(studentsList[index].id, absenteesList),
currentlyMarkedStudentId: -1,
isLoading: false,
);
});
}
@override
Widget construct(BuildContext context) {
var bloc = BlocProvider.of(context);
bloc.add(InitialEvent(
absenteeList: classAttendanceDetails.absentees,
studentsList: studentsList,
course: course));
return BlocBuilder(
builder: (BuildContext context, MarkAttendanceState state) {
var nextView;
state.when(
loadingStudent: (int? presentCount, int absentCount,
Record absenteesList, int studentId) {
nextView = loadingStudent(absenteesList, studentId);
},
loaded: (int presentCount, int absentCount, bool localOperation,
Record absenteesList) {
nextView = loadedStudents(absenteesList);
},
error: (int? presentCount, int? absentCount, String errorMessage) {
nextView = Textual content(errorMessage);
},
loading: (
int? presentCount,
int? absentCount,
) {
nextView = CircularProgressIndicator();
},
);
return nextView;
});
}
I count on nothing to occur when the nextView
modifications from loadedStudents
to loadingStudent
after which once more the loadedStudents
widget because the ListView.separated
is returned from each the widgets, therefore having the identical kind, and so they have the identical key connected, however I see the kid widget states are up to date. When the absenteeList
is modified.
So this made me interested in how rebuilding truly occurs when widgets are supplied a key and likewise when they don’t seem to be supplied any key.