我正在嘗試構建和問答應用程式。在我的問題頁面中,我正在通過一種方法構建我的答案。這就是方法;
SizedBox buildAnswerBox(var questionsOfSports,int answers) {
String? answerT;
return SizedBox(
height: 40,
width: 350,
child: StreamBuilder(stream: questionsOfSports.snapshots(),
builder: (BuildContext context, AsyncSnapshot asyncsnapshot) {
List<dynamic> question1 = asyncsnapshot.data.data()['q1']['q1Answers'];
switch (answers) {
case 1 :{ answerT=question1[0];}
break;
case 2 : {answerT=question1[1];}
break;
case 3 : {answerT=question1[2];}
break;
case 4 : {answerT=question1[3];}
}
return TextButton(
onPressed: () {},
child: Text('$answerT'),
style: TextButton.styleFrom(
backgroundColor: Colors.black,
),
);
},),
);
字串 answerT 保存來自有狀態小部件的引數
buildAnswerBox(questionsOfSports,4),
現在我想為每個問題和這些問題的答案回圈我的資料,但我不知道如何。
uj5u.com熱心網友回復:
尚不完全清楚什么是型別questionsOfSports,但假設它是某種型別 T.snapshots()的流,則在StreamBuilder.
如果questionsOfSports是串列,則需要將該串列轉換為流(請參閱此處)。
綜上所述,對我來說,創建此小部件的 Stream/StreamBuilder 是實作此目的的最佳方式并不明顯 - 回圈遍歷問題/答案串列并每次創建一個新小部件可能更簡單更容易一個按鈕被按下。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395285.html
