【发布时间】:2020-02-06 09:13:59
【问题描述】:
我正在尝试通过从 API 获取值来填充下拉列表。 当我单击下拉菜单时,正在显示值,当我尝试选择一个值时,会引发以下错误
items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.
列表中的值不为空
当我尝试删除下拉菜单的“值”属性时,错误未显示,但下拉菜单未显示所选值
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:something/Utils/formServices.dart';
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
import 'package:intl/intl.dart';
import 'dart:convert';
String insurer;
String package;
List insCat = List();
List insurers = List();
List<DropdownMenuItem> items = [];
class AddOrEditPack extends StatefulWidget{
@override
AddOrEditPackState createState() =>AddOrEditPackState();
}
class AddOrEditPackState extends State<AddOrEditPack>{
final formKey = GlobalKey<FormState>();
String insuranceCategory = ' ';
@override
void initState() {
getCategories();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Contact Us'),
),
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text("Home"),
),
],
),
),
body:
Form(
key:formKey,
child: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
child: DropdownButtonFormField<String>(
hint:Text('Insurance Category'),
items: insCat.map((item) {
return new DropdownMenuItem<String>(
child: new Text(item['name']),
// value: item['name'],
);
}).toList(),
onChanged: (String newValue) {
setState(() { insuranceCategory = newValue; });
print(newValue);
},
// value: insuranceCategory,
),
),
Padding(
padding: EdgeInsets.fromLTRB(10, 10, 10,0),
child: RaisedButton(
onPressed: () {
contact();
},
child: Text('Submit'),
),
)
],
),
)
),
);
}
getCategories() async{
var resp = await
http.get('http://192.168.4.101:3000/category/getCategoryList');
print(resp.body);
insCat = json.decode(resp.body);
setState(() {
insuranceCategory=insCat[0]['name'];
});
}
}
这是 API 返回的内容。
[
{"_id":"5d8dad3a2fcb272b7c0e74b5","name":"life insurance"},
{"_id":"5d8dad502fcb272b7c0e74b6","name":"vehicle insurance"},
{"_id":"5d8dadb22fcb272b7c0e74b9","name":"life insurance"}
]
【问题讨论】:
标签: flutter dart flutter-layout