gpt4 book ai didi

flutter - flutter 正确打印有 future map 的 map

转载 作者:行者123 更新时间:2023-12-03 04:34:05 25 4
gpt4 key购买 nike

有人可以提供此代码帮助吗?
我必须在下拉列表中将查询结果打印为列表
错误 flutter 回给我的是:

The following NoSuchMethodError was thrown buildingDropDownFormField(dirty, dependencies: [_FormScope], state:FormFieldState#e264a): Class 'Future<List>' has noinstance method '[]'. Receiver: Instance of 'Future<List>'Tried calling: The relevant error-causing widget was:
DropDownFormField


我已经尝试过getAllBrockers和getAllBrockers2的方式
class _AutomationAddScreen extends State<AutomationAdd> {
DataRepository _repository;
final _formKey = GlobalKey<FormState>();
String _broN = "";
Future<Map> _broMap;
Future<List> _bro;

static Future<List> getAllBrockers2(Future<List<Brockers>> l) async {
List q;
List<Brockers> b = await l;
for (var i = 0; i < b.length; i++) {
q.add(DataList(b[i].name, b[i].id));
}
return q;
}

static Future<Map> getAllBrockers(Future<List<Brockers>> l) async {
Map hBro;
Map m;
List<Brockers> b = await l;
for (var i = 0; i < b.length; i++) {
m = {"display": b[i].name, "value": b[i].id};
hBro.addAll(m);
}
return hBro;
}

@override
void initState() {
super.initState();
_repository = SqlRepository();
//_broMap = getAllBrockers(_repository.getAllBrockers());
_bro = getAllBrockers2(_repository.getAllBrockers());
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Form(
key: _formKey,
child: ListView(
children: <Widget>[
//SELECT BROCKER
Padding(
padding: EdgeInsets.only(top: 16),
child: DropDownFormField(
titleText: 'Connessione',
hintText: 'Selezionane uno',
value: _broN,
//dataSource: [_broMap],
dataSource: [_bro],
textField: 'display',
valueField: 'value',
onChanged: (display) {
setState(() {
_broN = display.toString();
});
},
),
),
],
),
),
));
}
}

class DataList {
String display;
int value;

DataList(this.display, this.value);

@override
String toString() {
return '{ ${this.display}, ${this.value} }';
}
}

最佳答案

您应该使用Future<List>Future<Map>而不是ListMap。该错误专门提到它无法在[]上调用Future<List>DropdownFormField无法创建其项列表,因为您提供了Future
您可以尝试使用以下方法-

class _AutomationAddScreen extends State<AutomationAdd> {
DataRepository _repository;
final _formKey = GlobalKey<FormState>();
String _broN = "";
Map _broMap; // New
List _bro = List<DataList>(); // New

static Future<void> getAllBrockers2() async { // New
final b = await _repository.getAllBrockers(); // New
for (var i = 0; i < b.length; i++) {
_bro.add(DataList(b[i].name, b[i].id)); // New
}
setState(() {}); // New
}

static Future<Map> getAllBrockers(Future<List<Brockers>> l) async {
Map hBro = {}; // New
Map m;
List<Brockers> b = await l;
for (var i = 0; i < b.length; i++) {
m = {"display": b[i].name, "value": b[i].id};
hBro.addAll(m);
}
return hBro;
}

@override
void initState() {
super.initState();
_repository = SqlRepository();
//_broMap = getAllBrockers(_repository.getAllBrockers());
getAllBrockers2(); // New
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Form(
key: _formKey,
child: ListView(
children: <Widget>[
//SELECT BROCKER
Padding(
padding: EdgeInsets.only(top: 16),
child: DropDownFormField(
titleText: 'Connessione',
hintText: 'Selezionane uno',
value: _broN,
//dataSource: [_broMap],
dataSource: [_bro],
textField: 'display',
valueField: 'value',
onChanged: (display) {
setState(() {
_broN = display.toString();
});
},
),
),
],
),
),
));
}
}

class DataList {
String display;
int value;

DataList(this.display, this.value);

@override
String toString() {
return '{ ${this.display}, ${this.value} }';
}
}

关于flutter - flutter 正确打印有 future map 的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64406777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com