Home > Net >  flutter: Is it possible to load a list defined by const from another file?
flutter: Is it possible to load a list defined by const from another file?

Time:11-08

I have defined a list called quiz in Question.dart. I need to load this list into main.dart and use it. And I want to use the id value of the quiz in main.dart. The class in main.dart is StatefulWidget. How can I solve this?

Question.dart

class Question {
  final int id;
  final String options;


  Question({required this.id, required this.options});
}

const List quiz = [
  {
    "id": 1,
    "options": 'hello',
  },
  {
    "id": 2,
    "options": 'good',
  },
  {
    "id": 3,
    "options": 'abc',
  },
];

main.dart

class Body extends StatefulWidget {
  @override
  _Body createState() => _Body();
}

class _Body extends State<Body> {

CodePudding user response:

Yes, you can use it because it's still declared as Globally.quiz defiled outside the Question{} scope. so you need to just import Question.dart file in main.dart and you can use the quiz list.

  • Related