Flutter, How to solve "Expected a value of type 'List<DropdownMenuItem<Object>>?', but got one of type 'List<dynamic>'

huangapple go评论64阅读模式
英文:

Flutter, How to solve "Expected a value of type 'List<DropdownMenuItem<Object>>?', but got one of type 'List<dynamic>'

问题

我正在将一组记录传递给自定义的下拉框,但出现了上面的错误。

// dataList是一个包含两个字段的记录列表:int id 和 String name

Widget DropDownList(dataList, val, ReturnSelectedValue) {
  return DropdownButton(
    value: val,
    isExpanded: true,
    items: dataList.map((item) {
      return DropdownMenuItem(
        value: item.id,
        child: Text(item.name!),
      );
    }).toList(),
    onChanged: (selectedvalue) {
      ReturnSelectedValue(selectedvalue);
    },
  );
}
英文:

I am passing a List of records to a custom DropDown but got the captioned error.

//dataList is a list of record with 2 fields: int id, and String name

Widget DropDownList(dataList, val, ReturnSelectedValue) {
  return DropdownButton(
    value: val,
    isExpanded: true,
    items: dataList.map((item) {
      return DropdownMenuItem(
        value: item.id,
        child: Text(item.name!),
      );
    }).toList(),
    onChanged: (selectedvalue) {
      ReturnSelectedValue(selectedvalue);
    },
  );
}

答案1

得分: 1

你可以使用“类型推断”来确保 dataList 的类型正确,方法是使用 List&lt;DropdownMenuItem&lt;T&gt;&gt; 构造函数。

Widget DropDownList(dataList, val, ReturnSelectedValue) {
  return DropdownButton(
    value: val,
    isExpanded: true,
    items: List&lt;DropdownMenuItem&lt;Object&gt;&gt;.from(dataList.map((item) {
      return DropdownMenuItem(
        value: item.id,
        child: Text(item.name!),
      );
    })),
    onChanged: (selectedvalue) {
      ReturnSelectedValue(selectedvalue);
    },
  );
}
英文:

You can use type inference to ensure that dataList is of the correct type by using the List&lt;DropdownMenuItem&lt;T&gt;&gt;.from constructor.

Widget DropDownList(dataList, val, ReturnSelectedValue) {
  return DropdownButton(
    value: val,
    isExpanded: true,
    items: List&lt;DropdownMenuItem&lt;Object&gt;&gt;.from(dataList.map((item) {
      return DropdownMenuItem(
        value: item.id,
        child: Text(item.name!),
      );
    })),
    onChanged: (selectedvalue) {
      ReturnSelectedValue(selectedvalue);
    },
  );
}

答案2

得分: 0

这是完整的示例:

<img src="https://i.stack.imgur.com/52cpM.gif" width="220">

**代码:**

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter App',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String selectedValue = '1';

  List<Data> dataList = [
    Data(id: '1', name: 'January'),
    Data(id: '2', name: 'February'),
    Data(id: '3', name: 'March'),
    Data(id: '4', name: 'April'),
    Data(id: '5', name: 'May'),
    Data(id: '6', name: 'June'),
    Data(id: '7', name: 'July'),
    Data(id: '8', name: 'August'),
    Data(id: '9', name: 'September'),
    Data(id: '10', name: 'October'),
    Data(id: '11', name: 'November'),
    Data(id: '12', name: 'December'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Moment Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            DropDownList(
              dataList: dataList,
              val: selectedValue,
              returnSelectedValue: (String) {
                setState(() {
                  selectedValue = String;
                });
                print('Selected value: $String');
              },
            ),
            Text(dataList.where((element) => element.id == selectedValue).first.name)
          ],
        ),
      ),
    );
  }
}

class Data {
  final String id;
  final String name;

  Data({required this.id, required this.name});
}

class DropDownList extends StatelessWidget {
  final List<Data> dataList;
  final String val;
  final Function(String) returnSelectedValue;

  const DropDownList({
    super.key,
    required this.dataList,
    required this.val,
    required this.returnSelectedValue,
  });

  @override
  Widget build(BuildContext context) {
    return DropdownButton(
      value: val,
      isExpanded: true,
      items: dataList.map((item) {
        return DropdownMenuItem(
          value: item.id,
          child: Text(item.name),
        );
      }).toList(),
      onChanged: (selectedValue) {
        returnSelectedValue.call(selectedValue!);
      },
    );
  }
}

希望这对您有帮助。

英文:

here is the full example:

<img src="https://i.stack.imgur.com/52cpM.gif" width="220">

Code:

import &#39;package:flutter/material.dart&#39;;
void main() =&gt; runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: &#39;Flutter App&#39;,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
_HomePageState createState() =&gt; _HomePageState();
}
class _HomePageState extends State&lt;HomePage&gt; {
String selectedValue = &#39;1&#39;;
List&lt;Data&gt; dataList = [
Data(id: &#39;1&#39;, name: &#39;January&#39;),
Data(id: &#39;2&#39;, name: &#39;February&#39;),
Data(id: &#39;3&#39;, name: &#39;March&#39;),
Data(id: &#39;4&#39;, name: &#39;April&#39;),
Data(id: &#39;5&#39;, name: &#39;May&#39;),
Data(id: &#39;6&#39;, name: &#39;June&#39;),
Data(id: &#39;7&#39;, name: &#39;July&#39;),
Data(id: &#39;8&#39;, name: &#39;August&#39;),
Data(id: &#39;9&#39;, name: &#39;September&#39;),
Data(id: &#39;10&#39;, name: &#39;October&#39;),
Data(id: &#39;11&#39;, name: &#39;November&#39;),
Data(id: &#39;12&#39;, name: &#39;December&#39;),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(&#39;Flutter Moment Demo&#39;),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
DropDownList(
dataList: dataList,
val: selectedValue,
returnSelectedValue: (String) {
setState(() {
selectedValue = String;
});
print(&#39;Selected value: $String&#39;);
},
),
Text(dataList.where((element) =&gt; element.id == selectedValue).first.name)
],
),
),
);
}
}
class Data {
final String id;
final String name;
Data({required this.id, required this.name});
}
class DropDownList extends StatelessWidget {
final List&lt;Data&gt; dataList;
final String val;
final Function(String) returnSelectedValue;
const DropDownList({
super.key,
required this.dataList,
required this.val,
required this.returnSelectedValue,
});
@override
Widget build(BuildContext context) {
return DropdownButton(
value: val,
isExpanded: true,
items: dataList.map((item) {
return DropdownMenuItem(
value: item.id,
child: Text(item.name),
);
}).toList(),
onChanged: (selectedValue) {
returnSelectedValue.call(selectedValue!);
},
);
}
}

huangapple
  • 本文由 发表于 2023年3月1日 12:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定