[DropdownButton] 应该只有一项
There should be exactly one item with [DropdownButton]'s
我想我想要的东西很简单,flutter 告诉我只有一个项目或者没有项目。但我确定项目不止一个,IDK,我应该如何针对这个问题提出问题。我只是想拿一个列表并用 CustomClass 的 items.I 做一个下拉菜单从 https://api.flutter.dev/flutter/material/DropdownButton-class.html 复制粘贴这行得通,但是当我用我的更改数据时......
import 'package:flutter/material.dart';
class BreedJson {
final String breed;
final String img;
BreedJson(this.breed, this.img);
}
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = 'default ';
final List<BreedJson> myBreedJson = [
BreedJson("Cavalier King Charles Spaniel",
'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'),
BreedJson('Curly-Coated Retriever',
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSLqpKzfZ6L0TSvUKBhXwJQOnqfWzoaQWoIjCZu1s0evNhfSFWeNVMYWJYt0MqInbznRgE&usqp=CAU')
];
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: myBreedJson
.map<DropdownMenuItem<String>>((BreedJson value) => DropdownMenuItem<String>(
value: value.breed,
child: Text(value.img),
))
.toList());
}
}
myBreedJson
列表不包含 default
这就是它抛出此错误的原因。
您可以在值上初始传递 null,为此使 dropdownValue
可为空
String? dropdownValue;
试试这个:
String dropdownValue = "Cavalier King Charles Spaniel";
我想我想要的东西很简单,flutter 告诉我只有一个项目或者没有项目。但我确定项目不止一个,IDK,我应该如何针对这个问题提出问题。我只是想拿一个列表并用 CustomClass 的 items.I 做一个下拉菜单从 https://api.flutter.dev/flutter/material/DropdownButton-class.html 复制粘贴这行得通,但是当我用我的更改数据时......
import 'package:flutter/material.dart';
class BreedJson {
final String breed;
final String img;
BreedJson(this.breed, this.img);
}
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = 'default ';
final List<BreedJson> myBreedJson = [
BreedJson("Cavalier King Charles Spaniel",
'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'),
BreedJson('Curly-Coated Retriever',
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSLqpKzfZ6L0TSvUKBhXwJQOnqfWzoaQWoIjCZu1s0evNhfSFWeNVMYWJYt0MqInbznRgE&usqp=CAU')
];
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: myBreedJson
.map<DropdownMenuItem<String>>((BreedJson value) => DropdownMenuItem<String>(
value: value.breed,
child: Text(value.img),
))
.toList());
}
}
myBreedJson
列表不包含 default
这就是它抛出此错误的原因。
您可以在值上初始传递 null,为此使 dropdownValue
可为空
String? dropdownValue;
试试这个:
String dropdownValue = "Cavalier King Charles Spaniel";