首页 > 解决方案 > 如何将选定的单选按钮组值保存在变量中以供以后使用?

问题描述

我正在尝试将我的单选按钮 JSON 数据保存在变量中,以便以后可以使用它。目前它没有显示任何错误。它只是显示默认值。

我在我的类中取了一个变量radioItemHolder,它保存了我的默认值。

class _DocBookingState extends State<DocBooking> {
String radioItemHolder = '03/09/2021';

然后我在 ListiewBuilder 下创建了单选按钮,在这里我将我的 JSON 数据作为单选按钮格式。

   Container(
                  child: FutureBuilder(
                    future: DocVisitDays(),
                    builder:
                        (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.connectionState !=
                          ConnectionState.done) {
                        return CircularProgressIndicator();
                      }
                      if (snapshot.hasError) {
                        return Text("Somthing went wrong");
                      }

                      if (snapshot.hasData) {
                        return ListView.builder(
                            scrollDirection: Axis.vertical,
                            physics: BouncingScrollPhysics(),
                            shrinkWrap: true,
                            itemCount: snapshot.data.length,
                            itemBuilder: (BuildContext context, int index) {
                              
                              return Container(
                                margin: EdgeInsets.all(10),
                                padding: EdgeInsets.all(10),
                                decoration: BoxDecoration(
                                  
                                  color: Colors.lightBlue[50],
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(12)),
                                  boxShadow: [
                                    BoxShadow(
                                      
                                      color: Colors.grey.withOpacity(0.9),
                                      offset: const Offset(
                                        0.0,
                                        5.0,
                                      ),
                                      blurRadius: 3.0,
                                      spreadRadius: 0.5,
                                    ),
                                  ],
                                ),
                                child: Row(
                                  children: [
                                    Radio(
                                      activeColor: Colors.green,
                                      value: index,
                                      groupValue: _radioValue,
                                      onChanged: (value) {
                                        setState(() {// Here Im trying to store the json value
                                           radioItemHolder = snapshot.data[index].visitDate;
                                          _radioValue = value as int;
                                        });
                                      },
                                    ),
                                   
                                    Text(
                                      "Visit Day: ${snapshot.data[index].visitDate},\n Fee: ${snapshot.data[index].fee},  \n Discounted Fee: ${snapshot.data[index].discountedFee},  \n Booking Fee: ${snapshot.data[index].bookingFee} ",
                                      style:
                                          TextStyle(fontFamily: 'Poppins'),
                                    ),
                                  ],
                                ),
                              );
                            });
                      }
                      return Text("Error while calling");
                    },
                  ),
                ),

我试图像这样将这个 JSON 数据保存${snapshot.data[index].visitDate}在我的变量中 仍然没有改变默认值。radioItemHolderradioItemHolder = snapshot.data[index].visitDate;


标签: jsonflutterdart

解决方案


我认为更改valueval有效。谁能告诉我原因?

 onChanged: (val) {
 setState(() {
 radioItemHolder = snapshot.data[index].visitDate;
_radioValue = val as int;
       });
},

推荐阅读