Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 无法从其他类访问方法_Flutter_Dart - Fatal编程技术网

Flutter 无法从其他类访问方法

Flutter 无法从其他类访问方法,flutter,dart,Flutter,Dart,我正在尝试访问另一个类的方法,因为我将类从StatefulWidget更改为StatefulWidget我无法访问StatefulWidget中的方法addItemToList,我想使用setState,虽然我知道它只适用于StatefulWidget或StatefulWidget。 这是我正在使用的代码,第一部分是我试图调用的方法的位置addItemToList: class ScoreScreen extends StatefulWidget { @override _ScoreSc

我正在尝试访问另一个类的方法,因为我将类从
StatefulWidget
更改为
StatefulWidget
我无法访问
StatefulWidget
中的方法addItemToList,我想使用setState,虽然我知道它只适用于
StatefulWidget
StatefulWidget
。 这是我正在使用的代码,第一部分是我试图调用的方法的位置
addItemToList

class ScoreScreen extends StatefulWidget {
  @override
  _ScoreScreenState createState() => _ScoreScreenState();
}

class _ScoreScreenState extends State<ScoreScreen> {
  
  String _nome;

  String get nome => this._nome;

  QuestionController _qnController = Get.put(QuestionController());

  final List<String> names = <String>[
    'Aby',
    'Aish',
    'Ayan',
    'Ben',
    'Bob',
    'Charlie',
    'Cook',
    'Carline'
  ];

  final List<int> msgCount = <int>[2, 0, 10, 6, 52, 4, 0, 2];

  List<int> auxInt = <int>[];

  List<String> auxString = <String>[];

  void addItemToList() async {
    
    setState(() {
          names.insert(0, _qnController.nome);
    msgCount.insert(0, _qnController.numOfCorrectAns);
    auxInt = msgCount;
    auxString = names;
    print(names);
      
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(....

虽然这是可行的,但如果您已经在使用GetX,为什么不将您的状态重构为一个控制器并脱离一个有状态的小部件呢?这似乎是一个好时机,因为这个国家似乎更全球化。
// We use get package for our state management

ScoreScreen score = Get.put(ScoreScreen());

class QuestionController extends GetxController
    with SingleGetTickerProviderMixin {
  // Lets animated our progress bar

  AnimationController _animationController;
  Animation _animation;
  // so that we can access our animation outside
  Animation get animation => this._animation;

  PageController _pageController;
  PageController get pageController => this._pageController;

  List<Question> _questions = sample_data.map((question) {
    // shuffle the options
    List<String> _options = question["options"];

    //_options.shuffle();

    return Question(
        id: question['id'],
        question: question['question'],
        options: _options,
        answer: question['answer'],
        bandeira: question['bandeira']);
  }).toList()
    ..shuffle(); // remove this shuffle to check if options are shuffled

  List<Question> get questions => this._questions;

  bool _isAnswered = false;
  bool get isAnswered => this._isAnswered;

  String _correctAns;
  String get correctAns => this._correctAns;

  String _selectedAns;
  String get selectedAns => this._selectedAns;

  int _indexQ;
  int get indexQ => this._indexQ;

  // for more about obs please check documentation
  RxInt _questionNumber = 1.obs;
  RxInt get questionNumber => this._questionNumber;

  String _nome;
  String get nome => this._nome;

  int _numOfCorrectAns = 0;
  int get numOfCorrectAns => this._numOfCorrectAns;

  // called immediately after the widget is allocated memory
  @override
  void onInit() {
    // Our animation duration is 60 s
    // so our plan is to fill the progress bar within 60s
    _animationController =
        AnimationController(duration: Duration(seconds: 60), vsync: this);
    _animation = Tween<double>(begin: 0, end: 1).animate(_animationController)
      ..addListener(() {
        // update like setState
        update();
      });

    // start our animation
    // Once 30s is completed go to the next qn
    _animationController.forward().whenComplete(nextQuestion);
    _pageController = PageController();

    super.onInit();

    if (_questionNumber == _questions.length) {
      reset();
      Get.to(() => WelcomeScreen());
      _animationController.reset();
    }
  }

  // // called just before the Controller is deleted from memory
  @override
  void onClose() {
    super.onClose();
    _animationController.dispose();
    _pageController.dispose();
  }

  void checkAns(Question question, int selectedIndex, List opcoes) {
    // because once user press any option then it will run
    _isAnswered = true;
    _correctAns = question.answer;

    _selectedAns = opcoes[selectedIndex];
    int index = opcoes.indexOf(_correctAns);

    if (selectedIndex == index) _numOfCorrectAns++;

    // It will stop the counter
    _animationController.stop();
    update();

    // Once user select an ans after 2s it will go to the next qn
    Future.delayed(Duration(seconds: 1), () {
      nextQuestion();
    });
  }

  void nextQuestion() {
    if (_questionNumber.value != _questions.length) {
      _isAnswered = false;
      _pageController.nextPage(
          duration: Duration(milliseconds: 250), curve: Curves.ease);

      // Reset the counter
      _animationController.reset();

      // Then start it again
      // Once timer is finish go to the next qn
      _animationController.forward().whenComplete(nextQuestion);
    } else {
      // Get package provide us simple way to naviigate another page

      // HERE I TRY TO CALL THE METHOD, but it is not working
      ScoreScreen().addItemToList();

      Get.to(() => ScoreScreen());
    }
  }