flutter - 当您返回更改第一个下拉菜单时,取决于 Dropdown 失败断言错误
问题描述
我正在尝试此视频中的代码:https ://www.youtube.com/watch?v= 4AUuhhSakro(或 github:https ://github.com/khaliqdadmohmand/flutter_dynamic_dropdownLists/blob/master/lib/main.dart )
问题是当我们(视频的观看者)尝试“返回”以更改初始状态(不是应用程序状态,如县或省)时,应用程序崩溃并出现以下错误:
There should be exactly one item with [DropdownButton]'s value: 14.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
我相信问题是当第一次构建下拉菜单时,它可以有一个空字符串作为value
下拉菜单的参数,但是第二次它在断言上崩溃(如果你将它设置为 null 你会崩溃,value==null
如果你不要重置您正在使用的变量,value
然后在新的下拉列表中这个值不在项目中。(计数必须是== 1
)
这在我自己的项目中也一直在绞尽脑汁,我以为我已经成功了,但显然它仍然很糟糕。
这是一个类似的问题,并且该解决方案中包含异步(但这对我来说根本不起作用)。
解决方案
您可以在下面复制粘贴运行完整代码
您可以onChanged
添加_myCity = null;
代码片段
onChanged: (String Value) {
setState(() {
_myState = Value;
_myCity = null;
_getCitiesList();
print(_myState);
});
},
工作演示
完整代码
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
_getStateList();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dynamic DropDownList REST API'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
alignment: Alignment.topCenter,
margin: EdgeInsets.only(bottom: 100, top: 100),
child: Text(
'KDTechs',
style: TextStyle(fontWeight: FontWeight.w800, fontSize: 20),
),
),
//======================================================== State
Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 5),
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
value: _myState,
iconSize: 30,
icon: (null),
style: TextStyle(
color: Colors.black54,
fontSize: 16,
),
hint: Text('Select State'),
onChanged: (String Value) {
setState(() {
_myState = Value;
_myCity = null;
_getCitiesList();
print(_myState);
});
},
items: statesList?.map((item) {
return DropdownMenuItem(
child: Text(item['name']),
value: item['id'].toString(),
);
})?.toList() ??
[],
),
),
),
),
],
),
),
SizedBox(
height: 30,
),
//======================================================== City
Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 5),
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
value: _myCity,
iconSize: 30,
icon: (null),
style: TextStyle(
color: Colors.black54,
fontSize: 16,
),
hint: Text('Select City'),
onChanged: (String Value) {
setState(() {
_myCity = Value;
print(_myCity);
});
},
items: citiesList?.map((item) {
return DropdownMenuItem(
child: Text(item['name']),
value: item['id'].toString(),
);
})?.toList() ??
[],
),
),
),
),
],
),
),
],
),
);
}
//=============================================================================== Api Calling here
//CALLING STATE API HERE
// Get State information by API
List statesList;
String _myState;
String stateInfoUrl = 'http://cleanions.bestweb.my/api/location/get_state';
Future<String> _getStateList() async {
await http.post(stateInfoUrl, headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}, body: {
"api_key": '25d55ad283aa400af464c76d713c07ad',
}).then((response) {
var data = json.decode(response.body);
// print(data);
setState(() {
statesList = data['state'];
});
});
}
// Get State information by API
List citiesList;
String _myCity;
String cityInfoUrl =
'http://cleanions.bestweb.my/api/location/get_city_by_state_id';
Future<String> _getCitiesList() async {
await http.post(cityInfoUrl, headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}, body: {
"api_key": '25d55ad283aa400af464c76d713c07ad',
"state_id": _myState,
}).then((response) {
var data = json.decode(response.body);
setState(() {
citiesList = data['cities'];
});
});
}
}
推荐阅读
- php - PHP 7.3 Laravel 数据库自动数字舍入错误
- javascript - 在javascript中的html模板字符串中将所有没有http url替换为http://?
- android - 收到警告“您的应用容易受到 Intent Redirection”的影响,但在预启动中没有发现错误
- amazon-web-services - 通过 CNAME 别名访问应用程序时,什么可能导致应用程序过时?
- sql - 存储过程 - 如果过程的任何部分失败,则不执行
- ios - iOS 13 - UISegmentedControl - 如何更改 selectedSegment 的圆角半径?
- c# - 如何计算数据表中所有行中特定的 [Math.pow] 列
- mysql - 如何处理返回没有行的 IN 子句的子查询
- php - 无法让我的 PHP 代码删除 mysql 中的行
- javascript - 确定圆上的所有点#Javascript