我想我想要一個這么簡單的東西,flutter 告訴我只有一個 item 或者沒有 item。但我確信有不止一個專案,IDK,我應該如何針對這個問題提出問題。我只想列出一個串列并使用 CustomClass 的專案制作一個下拉選單。我確實從![應該只有一項帶有 [DropdownButton] 的](https://img.uj5u.com/2022/05/23/79e56431b3de4f57963e04b083c68e8d.jpg)
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());
}
}
uj5u.com熱心網友回復:
試試這個:
String dropdownValue = "Cavalier King Charles Spaniel";
uj5u.com熱心網友回復:
myBreedJson串列不包含default 這就是它引發此錯誤的原因。
您可以最初傳遞 null 值,因為這可以為dropdownValuenull
String? dropdownValue;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/479978.html
