从 void 调用 ListView

Call a ListView from void

我对 Flutter 代码有疑问。我想从 void randAnswer() 调用 ListView aAntwort()。 ListView 应在 RaisedButton 中显示为文本。 模拟显示“NoSuchMethodError:方法'_aAntwort()'被调用为空。接收器:空......“

谁能帮帮我? 谢谢:)

                                children: <Widget>[                     //StatefulWidget
                                  RaisedButton(
                                      onPressed: () {},
                                      child:randAnswer()._aAntwort()),    //ERROR
                                ]

  



  void randAnswer() {                                                 //Random Answer 
    var list = <String>['aAnswer', 'bAnswer', 'cAnswer', 'dAnswer'];
    var rand = new Random();    
    //get the random string
    int i = rand.nextInt(list.length);
    String iAnswer = list[i];    
    //print the random String    
    print(iAnswer);

    //remove the random string
    list.removeAt(i);

    //print rest of the list
    print(list.toString());    
    //get the random string
    int ii = rand.nextInt(list.length);
    String iiAnswer = list[ii];    
    //print the random String    
    print(iiAnswer); 

    //remove the random string
    list.removeAt(ii);  

    //print rest of the list
    print(list.toString());    
    //get the random string
    int iii = rand.nextInt(list.length);
    String iiiAnswer = list[iii];    
    //print the random String    
    print(iiiAnswer); 

    //remove the random string
    list.removeAt(iii);

    //print rest of the list
    print(list.toString());    
    //get the random string
    int iiii = rand.nextInt(list.length);
    String iiiiAnswer = list[iiii];    
    //print the random String    
    print(iiiiAnswer);   

    //remove the random string
    list.removeAt(iiii); 

    //print rest of the list
    print(list.toString());




    ListView _aAntwort(){                   // I want to call this one
      if (iAnswer == "aAnswer") {
        return aAnswer();
      } else if (iAnswer == "bAnswer") {
        return bAnswer();
      } else if (iAnswer == "cAnswer") {
        return cAnswer();
      } else if (iAnswer == "dAnswer") {
        return dAnswer();    
    }

  }





  ListView aAnswer() {                    
    return ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      itemCount: count,
      itemBuilder: (BuildContext context, int position) {
        return Card(
          color: Colors.white,
          elevation: 2.0,
          child: ListTile(
            title: Text(this.todoList[position].aright,
                style: TextStyle(fontWeight: FontWeight.bold)),
            onTap: () {
              debugPrint("ListTile Tapped");
            },
          ),
        );
      },
    );
  }

  ListView bAnswer() {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      itemCount: count,
      itemBuilder: (BuildContext context, int position) {
        return Card(
          color: Colors.white,
          elevation: 2.0,
          child: ListTile(
            title: Text(this.todoList[position].bwrong,
                style: TextStyle(fontWeight: FontWeight.bold)),
            onTap: () {
              debugPrint("ListTile Tapped");
            },
          ),
        );
      },
    );
  }

  ListView cAnswer() {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      itemCount: count,
      itemBuilder: (BuildContext context, int position) {
        return Card(
          color: Colors.white,
          elevation: 2.0,
          child: ListTile(
            title: Text(this.todoList[position].cwrong,
                style: TextStyle(fontWeight: FontWeight.bold)),
            onTap: () {
              debugPrint("ListTile Tapped");
            },
          ),
        );
      },
    );
  }

  ListView dAnswer() {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      itemCount: count,
      itemBuilder: (BuildContext context, int position) {
        return Card(
          color: Colors.white,
          elevation: 2.0,
          child: ListTile(
            title: Text(this.todoList[position].dwrong,
                style: TextStyle(fontWeight: FontWeight.bold)),
            onTap: () {
              debugPrint("ListTile Tapped");
            },
          ),
        );
      },
    );
  }

void 函数的返回值始终为 null。

您可以编辑函数的返回类型

ListView randAnswer 
//or 
Widget randAnswer

最后一行应该是 return _aAntwort() 来调用函数,但是我建议你将 _aAntwort 函数形式分开 randAnswer 并使用 iAnswer 作为全局变量,因为您正在将其用作按钮调用。