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
After passing the values as parameters to your stateful widget you can access them by:
You should never create a
StatefulWidget
widget while using GetX because in GetX you can basically do anything usingStatelessWidget
.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
Now you can set the controller values before navigating to the other screen like this:
View
But try to use
StatelessWidgets
and pass data usingarguments
in GetX. There’s also documentation available onpub.dev
GetX package.