在下拉式颤振中设置所选值

时间:2019-12-10 08:10:22

标签: flutter

我有这个脚本可以构建下拉列表

var date = DateTime.now();
print(date.toString()); // prints something like 2019-12-10 10:02:22.287949
print(DateFormat('EEEE').format(date)); // prints Tuesday
print(DateFormat('EEEE, d MMM, yyyy').format(date)); // prints Tuesday, 10 Dec, 2019
print(DateFormat('h:mm a').format(date)); // prints 10:02 AM

这是我的Future<List<AddressCst>> _address; AddressCst selectedAddress; Widget addressDropdown() { return FutureBuilder<List<AddressCst>>( future: _address, initialData: [], builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.active || snapshot.connectionState == ConnectionState.waiting) { return Center( child: Container( width: 20, height: 20, margin: EdgeInsets.all(10), child: CircularProgressIndicator( backgroundColor: Configuration.PrimaryColor, ), )); } else if (snapshot.hasData) { return DropdownButton<AddressCst>( value: selectedAddress, isExpanded: true, items: snapshot.data.map((value) { return DropdownMenuItem<AddressCst>( value: value, child: Text( value.addressName, style: TextStyle(fontSize: 17), ), ); }).toList(), hint: Text( "Select Address", style: TextStyle(fontSize: 17), ), onChanged: (AddressCst choosen) { setState(() { selectedAddress = choosen; }); }, ); } else if (snapshot.hasError) { return Text("${snapshot.error}"); } return Container(width: 0.0, height: 0.0); }); }

AddressCst

所以我想设置class AddressCst { String deliveryAddressId; String userId; String addressName; String addressProvinceId; String addressCityId; String addressRemark; String addressLongitude; String addressLatitude; String cityName; String provinceName; AddressCst({ this.deliveryAddressId, this.userId, this.addressName, this.addressProvinceId, this.addressCityId, this.addressRemark, this.addressLongitude, this.addressLatitude, this.cityName, this.provinceName, }); factory AddressCst.fromJson(Map<String, dynamic> json) => AddressCst( deliveryAddressId: json["deliveryAddressId"] == null ? null : json["deliveryAddressId"], userId: json["userId"] == null ? null : json["userId"], addressName: json["addressName"] == null ? null : json["addressName"], addressProvinceId: json["addressProvinceId"] == null ? null : json["addressProvinceId"], addressCityId: json["addressCityId"] == null ? null : json["addressCityId"], addressRemark: json["addressRemark"] == null ? null : json["addressRemark"], addressLongitude: json["addressLongitude"] == null ? null : json["addressLongitude"], addressLatitude: json["addressLatitude"] == null ? null : json["addressLatitude"], cityName: json["cityName"] == null ? null : json["cityName"], provinceName: json["provinceName"] == null ? null : json["provinceName"], ); } ,所以我要从API获取数据,这就是我要做的

default value

但是当我运行它时,出现此错误

  

以下断言被抛出   FutureBuilder>(脏,状态:   _FutureBuilderState>#d4ee6):'package:flutter / src / material / dropdown.dart':断言失败:行   620 pos 15:“项目== null || items.isEmpty ||值== null ||   items.where(((DropdownMenuItem item)=> item.value == value).length   == 1':不正确。

我该如何解决?我想念什么吗?

0 个答案:

没有答案