elementzero23
elementzero23

Reputation: 1429

Flutter how to correctly pass data to StatefulWidget

My goal is to write a flutter app that manages courses and students who are assigned to the courses. I have a Homeview where I can select the courses by tapping on corresponding buttons. After tapping the button a CourseView is shown. The CourseView is an overview of the selected course where you can switch between a seating plan and a list of students.

The students and the courses are stored in an sqlite database.

I am passing the course that the user tapped on to the CourseView as route argument. I want to create the tabs (= list of widgets) for seating plan and students list inside the initState() method. Therefore I call the function getStudentsForCourse() just before to load all the students assigned to this course.

class _CourseViewState extends State<CourseView> {
  DBHelper dbHelper;
  Future<List<Student>> studentsListFuture;
  List<Widget> tabs = [];

  @override
  initState() {
    super.initState();
    dbHelper = DBHelper.dbHelper;

    // now load all the students for this course
    studentsListFuture = dbHelper.getStudentsForCourse(currentCourse);
    // and then add the seating plan
    tabs.add(
      // seating plan view
    );
    // ...and the list
    tabs.add(
      // students list view
    );
  }

  @override
  Widget build(BuildContext context) {
    // only here I can retrieve the arguments of the route
    final Course currentCourse = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      appBar: AppBar(
        title: Text(course.name),
      ),
       body: tabs[currentIndex],
       bottomNavigationBar: BottomNavigationBar(
         //navigate through the tabs
       ),
    );
  }

}

This does not work because when calling getStudentsForCourse() I don't have the course yet. How can I solve this? I think I am not doing it the right way.

EDIT: What @MSARKrish suggested would work for me. But I am using routes that are defined in my MaterialApp:

return MaterialApp(
      title: 'title',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeView(),
        '/CourseView': (context) => CourseView(),
      },
    );

Can I keep the routes and pass the Course as parameter to the constructor of CourseView?

Upvotes: 0

Views: 56

Answers (1)

MSARKrish
MSARKrish

Reputation: 4144

You are passing course that user selected to that Course Widget. You can use that variable inside state class as widget.course

Example: You can pass selectedCourse like this:

Navigator.push(context,MaterialPageRoute(builder: (context){return CourseView(selectedCourse: course);})); 

And you can get like this:

class CourseView extends StatefulWidget {

 final String selectedCourse;
 CourseView({this.selectedCourse});
 @override
 _CourseViewState createState() => _CourseViewState();
 }

 class _CourseViewState extends State<CourseView> {

  void initState() {
    print(widget.selectedCourse);

    }

   @override
   Widget build(BuildContext context) {
     return Scaffold();
 }
 }

Upvotes: 1

Related Questions