问题描述
您好,我正在使用Flutter下拉菜单.第一个位置菜单.第二个用于转移.单击第一个位置是可行的,但是当更改为第二个位置并单击第二个位置时,此错误将显示为'package:flutter/src/material/dropdown.dart':失败的断言:620行pos 15:'item == null ||items.isEmpty ||值== null || items.where(((DropdownMenuItem item)=> item.value ==值).length == 1':不正确."
Hello i am work with flutter drop down menu . firs menu for location . and second for sublocation . it is work when click on first location but when change to second and click second location this error appear "'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true."
我的变量
//location
String _myLocation ;
List data = List();
//sublocation
String _mySublocation ;
List data2 = List();
定位功能
Future getLocation() async {
try{
var res = await http
.get(Uri.encodeFull("http://iraqdoctors.com/api/locations"), headers: {"Accept": "application/json"});
setState(() {
var resBody = json.decode(res.body);
data = resBody;
});
return "Sucess";
}catch(e){
}
}//get location
分区功能
Future getSublocation() async {
try{
var res2 = await http
.post(Uri.encodeFull("http://iraqdoctors.com/api/getlocationflutter"),
headers: {"Accept": "application/json"},
body: {
"cityname":"$_myLocation",
}
);
setState(() {
var resBody2 = json.decode(res2.body);
data2 = resBody2;
});
return "Sucess";
}catch(e){
}
}//get sublocation
位置下拉列表
Container(
padding: EdgeInsets.only(right:10),
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
child:DropdownButton( //location
icon: Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.black, fontSize: 18),
value: _myLocation,
items: data.map((item) {
return new DropdownMenuItem(
child: Text(item['city'],style: TextStyle(color: Colors.black)),
value: item['city'],
);
}).toList(),
onChanged: ( newVal) {
setState(() {
_myLocation = newVal;
getSublocation();
});
},
isExpanded: true,
underline: SizedBox(),
iconEnabledColor: Colors.white,
hint: Text('اختر المدينة',style: TextStyle(color: Colors.black),),
),),
子位置下拉列表
Container(
padding: EdgeInsets.only(right:10),
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
child: DropdownButton(//sublocation
icon: Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.black, fontSize: 18) ,
items: data2.map((item) {
return new DropdownMenuItem(
child: Text(item['state'],style: TextStyle(color: Colors.black)),
value: item['id'].toString(),
);
}).toList(),
onChanged: (value) {
setState(() {
_mySublocation = value ;
});
},
value: _mySublocation ,
isExpanded: true,
hint: Text('اختر المنطقة',style: TextStyle(color: Colors.black),),
underline: SizedBox(),
iconEnabledColor: Colors.white,
),),
```
推荐答案
您可以在
下复制粘贴运行完整代码我使用2秒的延迟来模拟您的http任务
错误消息表示 data2
为空
在您的 onChanged
中,您需要等待 await getSublocation()
并确保将json转换为 data2
是正确的
You can copy paste run full code below
I use 2 seconds delay to simulate your http task
The error message means data2
is null
In your onChanged
, you need to await await getSublocation()
and make sure convert json to data2
is correct
代码段
onChanged: (newVal) async {
_myLocation = newVal;
await getSublocation();
setState(() {});
},
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String _myLocation = null;
List data = List();
//sublocation
String _mySublocation = null;
List data2 = List();
Future getLocation() async {
try {
/* var res = await http.get(
Uri.encodeFull("http://iraqdoctors.com/api/locations"),
headers: {"Accept": "application/json"});
setState(() {
var resBody = json.decode(res.body);
data = resBody;
});*/
await Future.delayed(const Duration(seconds: 1), () {});
data.clear();
data.add({"city": "A"});
data.add({"city": "B"});
setState(() {});
return "Sucess";
} catch (e) {}
} //get location
Future getSublocation() async {
try {
/*var res2 = await http.post(
Uri.encodeFull("http://iraqdoctors.com/api/getlocationflutter"),
headers: {
"Accept": "application/json"
},
body: {
"cityname": "$_myLocation",
});
setState(() {
var resBody2 = json.decode(res2.body);
data2 = resBody2;
});*/
await Future.delayed(const Duration(seconds: 2), () {});
String jsonString = '''
[ { "id": 1, "state": "الحارثية", "city": "بغداد", "created_at": "2019-12-17 07:20:16", "updated_at": "2019-12-17 07:20:16" } ]
''';
data2.clear();
data2 = json.decode(jsonString);
/*data2.clear();
data2.add({"state": "A1", "id": "a1"});
data2.add({"state": "B1", "id": "b1"});*/
print(data2.toString());
print(data2[0]['state'].toString());
setState(() {});
return "Sucess";
} catch (e) {
print(e.toString());
}
} //get sublocation
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
getLocation();
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.only(right: 10),
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
child: DropdownButton(
//location
icon: Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.black, fontSize: 18),
value: _myLocation,
items: data.map((item) {
return new DropdownMenuItem(
child: Text(' ${item['city']} ',
style: TextStyle(color: Colors.black)),
value: item['city'],
);
}).toList(),
onChanged: (newVal) async {
_myLocation = newVal;
await getSublocation();
setState(() {});
},
isExpanded: true,
underline: SizedBox(),
iconEnabledColor: Colors.white,
hint: Text(
'اختر المدينة',
style: TextStyle(color: Colors.black),
),
),
),
Container(
padding: EdgeInsets.only(right: 10),
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
child: DropdownButton(
//sublocation
icon: Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.black, fontSize: 18),
items: data2.map((item) {
return new DropdownMenuItem(
child: Text(' ${item['state']}',
style: TextStyle(color: Colors.black)),
value: item['id'].toString(),
);
}).toList(),
onChanged: (value) {
setState(() {
_mySublocation = value;
});
},
value: _mySublocation,
isExpanded: true,
hint: Text(
'اختر المنطقة',
style: TextStyle(color: Colors.black),
),
underline: SizedBox(),
iconEnabledColor: Colors.white,
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
这篇关于Flutter:使用拖曳下拉按钮从另一个加载时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!