I have a page where it shows ListView that contains list of schools. So I load all the schools (in initState() method) when the user clicks on My Schools.
Now I want to pass the School object when user clicks on a school's in the ListView and show the edit school page.
I want to use the provider patter for this so I can pass a single school object to the edit page and once user done with edit and press back, it automatically reflects the updated school in the ListView.
I am really confused with how to pass a single object from a list to the edit page. Following is my code where I have model, provider and state classes. As I am not getting how to access the school object in the edit page I have not created it yet.
I do not want to use the constructor to pass the school object to edit.
// School model class
class SchoolModel {
String name;
double points;
}
// school provider
class SchoolProvider with ChangeNotifier {
School _school;
School get school => _school;
set School(School value) {
_school = value;
notifyListeners();
}
}
// School list page
class ListSchool extends StatefulWidget {
@override
_ListSchoolState createState() => _ListSchoolState();
}
// List page state
class _ListSchoolState extends State<ListSchool> {
List<School> _schools;
@override
void initState() {
_schools = FirebaseCall(); // loading schools
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => SchoolProvider(),
),
],
child: ListView.builder(
itemCount: _schools.length,
itemBuilder: (BuildContext ctxt, int index) {
return ListTile(
title: Text(_schools[index].name);
onTap() {
// Open Edit page and pass the clicked school object to edit page
}
);
}
)
)
);
Thank you