【问题标题】:Flutter : DropDown menue dont work with dynamic valuesFlutter:下拉菜单不适用于动态值
【发布时间】:2021-12-20 05:58:01
【问题描述】:

我创建下拉列表作为小部件,使用提供程序更改值但给我一个错误,小部件是这样的:

FutureBuilder(
      future: ApiService().getTeachersLectures(username, token),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          var lectures = snapshot.data;
          return Consumer<LecturesProvider>(
              builder: (context, lectureProvider, child) {
            return DropdownButton(
                 value: lectureProvider.index,
                 onChanged: (val) {
                 lectureProvider.changeIndex(val);
                },
                items: lectures.map<DropdownMenuItem>((e) {
              return DropdownMenuItem<String>(
                  value: e.lecture, child: Text('${e.lecture}'));
            }).toList());
          });
        } else {
          return const Text('not ok');
        }
      },
    )

而且提供者很简单:

class LecturesProvider with ChangeNotifier {
  var index = 0;
  void changeIndex(val) {
    index = val;
    notifyListeners();
  }
}

是这个错误:

type 'List' 不是 type'List 的子类型?'

【问题讨论】:

    标签: flutter drop-down-menu provider flutter-futurebuilder


    【解决方案1】:

    这个错误是因为你List&lt;DropdownMenuItem&gt; 不是空安全引起的。 如果您确定此列表永远不会为空,只需添加一个空检查 !

    【讨论】:

    • 谢谢但我发现了错误,只是把DropDownButton的类型放了
    猜你喜欢
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多