如何在颤振中实现下拉列表? [英] How to implement drop down list in flutter?

查看:35
本文介绍了如何在颤振中实现下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位置列表,我想在 Flutter 中将其实现为下拉列表.我对这门语言很陌生.这是我所做的.

I have a list of locations that i want to implement as a dropdown list in Flutter. Im pretty new to the language. Here's what i have done.

new DropdownButton(
  value: _selectedLocation,
  onChanged: (String newValue) {
    setState(() {
      _selectedLocation = newValue;
     });
},
items: _locations.map((String location) {
  return new DropdownMenuItem<String>(
     child: new Text(location),
  );
}).toList(),

这是我的物品清单:

List<String> _locations = ['A', 'B', 'C', 'D'];

我收到以下错误.

Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 468 pos 15: 'value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

我假设 _selectedLocation 的值变为 null.但我是这样初始化的.

I assume the value of _selectedLocation is getting null. But i am initialising it like so.

String _selectedLocation = '请选择一个位置';

推荐答案

试试这个

DropdownButton<String>(
  items: <String>['A', 'B', 'C', 'D'].map((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
  onChanged: (_) {},
)
    

这篇关于如何在颤振中实现下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆