下拉列表不适用于字符串列表
Dropdown not working with List of strings flutter
我在设置下拉菜单功能时遇到了一些问题。我不明白这是为什么,因为小部件本身接受 String: DropdownMenuItem
我收到错误:
type string is not a subtype of type DropdownMenuItem<dynamic>
我尝试将我的值转换为字符串或动态值,如下所示:
值:map["breed"].cast() 或
值:地图[“品种”].cast()
但似乎没有任何效果。
class DogForm extends StatefulWidget {
@override
_DogFormState createState() => _DogFormState();
}
class _DogFormState extends State<DogForm> {
final todoController = TextEditingController();
final List<DropdownMenuItem> breeds = [];
String? _mySelection = '';
final List<Map> _mybreedJson = [
{
"breed": "Cavalier King Charles Spaniel",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/CarterBIS.Tiki.13.6.09.jpg/220px-CarterBIS.Tiki.13.6.09.jpg"
},
{
"breed": "Curly-Coated Retriever",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Curly_Coated_Retriever.jpg/220px-Curly_Coated_Retriever.jpg"
},
];
void addToDo() async {
if (todoController.text.trim().isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Empty title"),
duration: Duration(seconds: 2),
));
return;
}
await saveTodo(todoController.text);
setState(() {
todoController.clear();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Parse Todo List"),
backgroundColor: Colors.blueAccent,
centerTitle: true,
),
body: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(17.0, 1.0, 7.0, 1.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
isDense: true,
hint: Text('Select Breed'),
value: _mySelection,
onChanged: (String? newValue) {
setState(() {
_mySelection = newValue;
});
},
items: _mybreedJson.map((Map map) {
return DropdownMenuItem<String>(
value: map["breed"],
// value: _mySelection,
child: Row(
children: <Widget>[
Image.asset(
map["img"],
width: 25,
),
Container(
margin: EdgeInsets.only(left: 10),
child: Text(map["breed"])),
],
),
);
}).toList(),
),
),
),
),
]),
)
],
));
}
}
Future<void> saveTodo(String title) async {
await Future.delayed(Duration(seconds: 1), () {});
final todo = ParseObject('Todo')
..set('title', title)
..set('done', false);
await todo.save();
}
拜托!有人可以告诉我如何使用这样的列表吗?
_mybreedJson = [
{
"breed": "Cavalier King Charles Spaniel",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/CarterBIS.Tiki.13.6.09.jpg/220px-CarterBIS.Tiki.13.6.09.jpg"
},
{
"breed": "Curly-Coated Retriever",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Curly_Coated_Retriever.jpg/220px-Curly_Coated_Retriever.jpg"
},
];
如果你制作 Map 地图 --> Map 地图,也许 flutter 会告诉你错误在哪里。
这就是你想要的?请运行代码。我更改了 .asset => .network 并且,我将实际值设置为默认值
import 'package:flutter/material.dart';
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
theme: ThemeData.dark(),
home: DogForm(),
);
}
}
class DogForm extends StatefulWidget {
@override
_DogFormState createState() => _DogFormState();
}
class _DogFormState extends State<DogForm> {
final todoController = TextEditingController();
final List<DropdownMenuItem> breeds = [];
String? _mySelection = 'Cavalier King Charles Spaniel';
final List<Map> _mybreedJson = [
{
"breed": "Cavalier King Charles Spaniel",
"img":
"https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
},
{
"breed": "Curly-Coated Retriever",
"img":
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSjiLOiEP-qSR6OgMrPELypnHToVToGPEc_qTkuLq5mMKwCCMoQ4x6Fsn19uvBoDO0qZaQ&usqp=CAU"
},
];
void addToDo() async {
if (todoController.text.trim().isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Empty title"),
duration: Duration(seconds: 2),
));
return;
}
await saveTodo(todoController.text);
setState(() {
todoController.clear();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Parse Todo List"),
backgroundColor: Colors.blueAccent,
centerTitle: true,
),
body: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.fromLTRB(17.0, 1.0, 7.0, 1.0),
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
isDense: true,
hint: const Text('Select Breed'),
value: _mySelection,
onChanged: (String? newValue) {
setState(() {
_mySelection = newValue;
});
},
items: _mybreedJson.map((Map map) {
return DropdownMenuItem<String>(
value: map["breed"],
// value: _mySelection,
child: Row(
children: <Widget>[
Image.network(
map["img"],
width: 25,
),
Container(margin: const EdgeInsets.only(left: 10), child: Text(map["breed"])),
],
),
);
}).toList(),
),
),
),
),
]),
)
],
));
}
}
Future<void> saveTodo(String title) async {
await Future.delayed(const Duration(seconds: 1), () {});
final todo = ParseObject('Todo')
..set('title', title)
..set('done', false);
await todo.save();
}
我在设置下拉菜单功能时遇到了一些问题。我不明白这是为什么,因为小部件本身接受 String: DropdownMenuItem 我收到错误:
type string is not a subtype of type DropdownMenuItem<dynamic>
我尝试将我的值转换为字符串或动态值,如下所示: 值:map["breed"].cast() 或 值:地图[“品种”].cast()
但似乎没有任何效果。
class DogForm extends StatefulWidget {
@override
_DogFormState createState() => _DogFormState();
}
class _DogFormState extends State<DogForm> {
final todoController = TextEditingController();
final List<DropdownMenuItem> breeds = [];
String? _mySelection = '';
final List<Map> _mybreedJson = [
{
"breed": "Cavalier King Charles Spaniel",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/CarterBIS.Tiki.13.6.09.jpg/220px-CarterBIS.Tiki.13.6.09.jpg"
},
{
"breed": "Curly-Coated Retriever",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Curly_Coated_Retriever.jpg/220px-Curly_Coated_Retriever.jpg"
},
];
void addToDo() async {
if (todoController.text.trim().isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Empty title"),
duration: Duration(seconds: 2),
));
return;
}
await saveTodo(todoController.text);
setState(() {
todoController.clear();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Parse Todo List"),
backgroundColor: Colors.blueAccent,
centerTitle: true,
),
body: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(17.0, 1.0, 7.0, 1.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
isDense: true,
hint: Text('Select Breed'),
value: _mySelection,
onChanged: (String? newValue) {
setState(() {
_mySelection = newValue;
});
},
items: _mybreedJson.map((Map map) {
return DropdownMenuItem<String>(
value: map["breed"],
// value: _mySelection,
child: Row(
children: <Widget>[
Image.asset(
map["img"],
width: 25,
),
Container(
margin: EdgeInsets.only(left: 10),
child: Text(map["breed"])),
],
),
);
}).toList(),
),
),
),
),
]),
)
],
));
}
}
Future<void> saveTodo(String title) async {
await Future.delayed(Duration(seconds: 1), () {});
final todo = ParseObject('Todo')
..set('title', title)
..set('done', false);
await todo.save();
}
拜托!有人可以告诉我如何使用这样的列表吗?
_mybreedJson = [
{
"breed": "Cavalier King Charles Spaniel",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/CarterBIS.Tiki.13.6.09.jpg/220px-CarterBIS.Tiki.13.6.09.jpg"
},
{
"breed": "Curly-Coated Retriever",
"img":
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Curly_Coated_Retriever.jpg/220px-Curly_Coated_Retriever.jpg"
},
];
如果你制作 Map 地图 --> Map
这就是你想要的?请运行代码。我更改了 .asset => .network 并且,我将实际值设置为默认值
import 'package:flutter/material.dart';
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
theme: ThemeData.dark(),
home: DogForm(),
);
}
}
class DogForm extends StatefulWidget {
@override
_DogFormState createState() => _DogFormState();
}
class _DogFormState extends State<DogForm> {
final todoController = TextEditingController();
final List<DropdownMenuItem> breeds = [];
String? _mySelection = 'Cavalier King Charles Spaniel';
final List<Map> _mybreedJson = [
{
"breed": "Cavalier King Charles Spaniel",
"img":
"https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
},
{
"breed": "Curly-Coated Retriever",
"img":
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSjiLOiEP-qSR6OgMrPELypnHToVToGPEc_qTkuLq5mMKwCCMoQ4x6Fsn19uvBoDO0qZaQ&usqp=CAU"
},
];
void addToDo() async {
if (todoController.text.trim().isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Empty title"),
duration: Duration(seconds: 2),
));
return;
}
await saveTodo(todoController.text);
setState(() {
todoController.clear();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Parse Todo List"),
backgroundColor: Colors.blueAccent,
centerTitle: true,
),
body: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.fromLTRB(17.0, 1.0, 7.0, 1.0),
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
isDense: true,
hint: const Text('Select Breed'),
value: _mySelection,
onChanged: (String? newValue) {
setState(() {
_mySelection = newValue;
});
},
items: _mybreedJson.map((Map map) {
return DropdownMenuItem<String>(
value: map["breed"],
// value: _mySelection,
child: Row(
children: <Widget>[
Image.network(
map["img"],
width: 25,
),
Container(margin: const EdgeInsets.only(left: 10), child: Text(map["breed"])),
],
),
);
}).toList(),
),
),
),
),
]),
)
],
));
}
}
Future<void> saveTodo(String title) async {
await Future.delayed(const Duration(seconds: 1), () {});
final todo = ParseObject('Todo')
..set('title', title)
..set('done', false);
await todo.save();
}