当我尝试将数据放入dropdownmenu时,它显示堆栈跟踪
类'UserSite'没有实例方法'[]'。 I / flutter(4454):接收方:“ UserSite”的实例 I / flutter(4454):尝试致电: I /颤振(4454): I / flutter(4454):引发异常时,这是 堆: I /颤振(4454):#0 Object.noSuchMethod (dart:core / runtime / libobject_patch.dart:50:5) I / flutter(4454):#1 _HomePage._dropDown .. (软件包:truein_one / src / screens / my_visitors.dart:209:48)
下面我提供了UserSite Model类的代码
// To parse this JSON data, do
// final client = clientFromJson(jsonString);
import 'dart:convert';
List<UserSite> clientFromJson(String str) =>
new List<UserSite>.from(json.decode(str).map((x) => UserSite.fromJson(x)));
String clientToJson(List<UserSite> data) =>
json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));
class UserSite {
String clientSiteId;
String siteName;
UserSite({
this.clientSiteId,
this.siteName,
});
factory UserSite.fromJson(Map<String, dynamic> json) => new
UserSite(
clientSiteId: json["client_site_id"],
siteName: json["site_name"],);
Map<String, dynamic> toJson() => {
"client_site_id": clientSiteId,
"site_name": siteName,
};
}
现在,我将提供已实现下拉菜单的Ui部分的代码
Widget _dropDown() {
return StreamBuilder<List<UserSite>>(
stream: blocs.subject.stream,
builder: (context, AsyncSnapshot<List<UserSite>> snapshot) {
if (snapshot.hasData) {
list = snapshot.data;
return Container(
child: new Column(
children: <Widget>[
new Container(
margin: Platform.isAndroid
? const EdgeInsets.only(top: 25.0, left: 0.0)
: const EdgeInsets.only(top: 45.0, left: 0.0),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
side: BorderSide(width: 1.5, style: BorderStyle.solid),
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
items: list.map((items) {
return new DropdownMenuItem<String>(
child: new Text(items["site_name"]),
value: items["client_site_id"],
);
}).toList(),
onChanged: (newVal) {
setState(() {
dropDownValue = newVal;
});
},
value: dropDownValue,
),
))
],
));
} else {
return new Container(
child: Text("Wait"),
);}
},
);
}
我的列表中有要提供image的数据
我不明白为什么会出现此错误。也许这是一个更简单的问题,但是我对编码和抖动非常陌生。请帮帮我。
答案 0 :(得分:0)
我通过强制转换我的班级名称解决了这个问题
Widget _dropDown() {
return StreamBuilder<List<UserSite>>(
stream: blocs.subject.stream,
builder: (context, AsyncSnapshot<List<UserSite>> snapshot) {
if (snapshot.hasData) {
return Container(
child: new Column(
children: <Widget>[
new Container(
margin: Platform.isAndroid
? const EdgeInsets.only(top: 25.0, left: 0.0)
: const EdgeInsets.only(top: 45.0, left: 0.0),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
side: BorderSide(width: 1.5, style: BorderStyle.solid),
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<UserSite>(
items: snapshot.data
.map<DropdownMenuItem<UserSite>>((UserSite value) {
return DropdownMenuItem<UserSite>(
child: new Text(value.siteName),
value: value,
);
}).toList(),
onChanged: (newVal) {
setState(() {
dropDownValue = newVal;
});
},
value: dropDownValue,
),
))
],
));
} else {
return CircularProgressIndicator();
}
},
);
}