skip to Main Content

I am Developing a Quiz.

Where User Choses Subject > Chapter

Then from Chapter Info Page > Start Quiz.

I am able to fetch the Subject ID and Chapter ID and When the User Clicks on Start Quiz Button Present in Chapter Info

Quiz Starts

Quiz is based on MVC Pattern

Here I am Redirecting user to Quiz Screen from Chapter Info page

How to access Value from Statefull widget ChapterInfo to a QuizContoller

onPressed: () {
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) => TopicWiseQuizScreen(quizSubjectID:subjectId,CatID: catId),
                                        ),
                                      );
                                    },



class TopicWiseQuizScreen extends StatelessWidget {

const TopicWiseQuizScreen({super.key, required quizSubjectID, required int CatID});

@override
Widget build(BuildContext context) {
TopicWiseQuestionController _controller = Get.put(TopicWiseQuestionController());
return Scaffold()}}

class TopicWiseQuestionController extends GetxController
with SingleGetTickerProviderMixin {

late final int subID;
late final int catID;

2

Answers


  1. After passing the values as parameters to your stateful widget you can access them by:

    widget.quizSubjectID;
    widget.catID;
    
    Login or Signup to reply.
  2. You should never create a StatefulWidget widget while using GetX because in GetX you can basically do anything using StatelessWidget.

    But if you really want to pass a value to controller from StatefulWidget then you can assign those values in while navigating to the view like this:

    Controller

    class DemoController extends GetxController {
      DemoController();
    
      Rx<int> quizSubjectID = 0.obs;
      Rx<int> catID = 0.obs;
    }
    

    Now you can set the controller values before navigating to the other screen like this:

    View

    onPressed: () {
           final logic = Get.put(DemoController());
           logic.quizSubjectID.value = subjectId;
           logic.catID.value = catId;
           Navigator.push(
           context,
           MaterialPageRoute(
           builder: (context) => TopicWiseQuizScreen(quizSubjectID:subjectId,CatID: catId),
                    ),
                );
           },
    

    But try to use StatelessWidgets and pass data using arguments in GetX. There’s also documentation available on pub.dev GetX package.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search