我想根据Flutter中另一个下拉菜单的选择在下拉菜单中显示数据

时间:2020-06-04 19:21:06

标签: flutter dart

  //Investment Asset Class
  String classInvestment;
  //Investment Goal
  String goalInvestment;

  List<dynamic> _classes = [];
  List<dynamic> _types = [];
  Widget _investClassWidget() {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 12),
        child: FutureBuilder<List<InvestmentModel>>(
          future: fetchData,
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.active:
              case ConnectionState.done:
                if (snapshot.hasData) {
                  snapshot.data.forEach((element) {
                    _classes.add(element);
                  });
                  return DropdownButton(
                    items: snapshot.data
                        .map((map) => DropdownMenuItem(
                              value: map.title,
                              child: Text(
                                map.title,
                                style: GoogleFonts.muli(
                                    textStyle: TextStyle(
                                        color: Colors.black,
                                        fontWeight: FontWeight.w600)),
                              ),
                            ))
                        .toList(),
                    underline: Divider(
                      color: Colors.transparent,
                    ),
                    value: classInvestment,
                    hint: Text(
                      '',
                      style: GoogleFonts.muli(
                          textStyle: TextStyle(
                              color: Colors.black,
                              fontSize: 20,
                              fontWeight: FontWeight.w600)),
                    ),
                    icon: Icon(
                      CupertinoIcons.down_arrow,
                      color: Colors.black,
                    ),
                    isExpanded: true,
                    onChanged: (value) => selectedChange(value),
                  );
                }
                return LinearProgressIndicator();
              case ConnectionState.waiting:
                return LinearProgressIndicator();
              default:
                return LinearProgressIndicator();
            }
          },
        ));
  }

  void selectedChange(String value) {
    setState(() {
      goalInvestment = 'Choose a goal';
      classInvestment = value;
      _types = List.from(_types)..addAll(getgoalByTitle(value));
      print(_types);
    });
  }

  getgoalByTitle(String value) => _classes
    .map((map) => map)
    .where((item) => item.title == value)
    .map((item) => item.types)
    .expand((i) => i)
    .toList();

  Widget _investTypeWidget() {
    return Container(
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.0),
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 6.0,
            offset: Offset(0, 2),
          ),
        ],
      ),
      padding: EdgeInsets.symmetric(horizontal: 12),
      child: DropdownButton(
        value: goalInvestment,
        disabledHint: Text(
          'Please select a class',
          style: GoogleFonts.muli(
              textStyle: TextStyle(fontWeight: FontWeight.w600)),
        ),
        items: _types.map((map) {
          return DropdownMenuItem(
            value: map['name'],
            child: Text(
              '${map['name']} (${map['return']}%)',
              style: GoogleFonts.muli(
                  textStyle: TextStyle(
                      color: Colors.black, fontWeight: FontWeight.w600)),
            ),
          );
        }).toList(),
        underline: Divider(
          color: Colors.transparent,
        ),
        icon: Icon(
          CupertinoIcons.down_arrow,
          color: Colors.black,
        ),
        isExpanded: true,
        onChanged: (value) {
          setState(() {
            goalInvestment = value;
            print(goalInvestment);
          });
        },
      ),
    );
  }

我正在从Firestore提取数据。我想显示两个下拉菜单,其中第二个下拉菜单项取决于第一个菜单项的选择。 我遇到的主要问题是,当我更改第一个下拉列表的选择时,第二个下拉列表的值为空。 但是,当我更改选择时,所需的数据会成功打印 这是错误:

There should be exactly one item with [DropdownButton]'s value: Choose a goal. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

0 个答案:

没有答案