flutter - Get values of dynamic list of widgets - flutter
问题描述
I've managed to make a dynamic questionnaire connecting flutter with Firestore.
My custom Question widgets are just a Text() holding the string of the question, and a Slider() so the user can give an answer from 1 to 5
Now that the questions are displayed, how should I get the values?
Here's my code:
var questions = <Widget>[];
//Async read to the DB
Future query() async {
var snap = await Firestore.instance
.collection("Forms")
.document(formID)
.get();
var arr = <Widget>[]; //just a temp holder of the widgets
for(String q in list){
arr.add(Question(min: 1.0, max: 5.0, question: q, value: 1.0,));
}
setState(() {
questions = arr;
});
}
And then in the build I'm rendering:
Scaffold(
body:Container(
child:Column(
children: <Widget>[
Text("Title"),
Column(
children: questions,
),
RaisedButton(
onPressed: () => sendFeedback(),
color: Colors.redAccent,
textColor: Colors.white,
child: Text("Send")
]
)
What would be the code for the sendFeedback() function? I would like to get the values for all the sliders in my children list and then write those to Firestore in only one call to the DB.
解决方案
You need to hold the state of all sliders. I'd recommend you the BLoC pattern for state management.
Edit: You can have a List<int>
to hold the values in your bloc and in every slider you will implement the function onChangeEnd: bloc.addValue
and send the slider value to the bloc which will add them to the list.
Then on the button you will have something like this onPressed: () => bloc.sendFeedback()
that will take the values from the list and write them to Firestore.
Keep in mind that this is a solution on the top of my head right now for you to understand the concept
推荐阅读
- python - Python切片数组获取除一些索引之外的所有内容
- javascript - 尝试在 discord.js 中记录消息
- javascript - 为 javascript 创建代码以包含“客户”对象
- r - 多次运行函数并将值存储在 R 中的向量中
- ajax - 模态不会以编程方式显示
- typescript - Vuex Typescript 我收到错误“成员 'someMutation' 隐含地具有 'any' 类型。” 在 Component.ts 文件中
- identityserver4 - IdentityServer4 添加 IdentityClaim
- python - 根据我的 df 在 for 循环中的长度重复数组的行 N 次不起作用。错误“numpy.ndarray”对象不可调用”(Python)
- android - 无法将匹配图像的结果用于下一个活动
- php - facebook php api 参数“place”必须是有效的地点标签 ID