【问题标题】:Flutter fetching DropdownButtonFormField list items from Sqflite - Error 0 or 2 valuesFlutter 从 Sqflite 获取 DropdownButtonFormField 列表项 - 错误 0 或 2 值
【发布时间】:2020-05-04 06:55:43
【问题描述】:

我正在尝试制作一个 dropdownButtonFormField,其中包含来自 sqflite 数据库的对象值列表。我到了会显示列表项的地步,但是当我单击其中一个时,它会喊出错误

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  Section currentSection;
  @override
  Widget build(BuildContext context) {
    final sectionsProvider = Provider.of<SectionsProvider>(context);
    return Scaffold(
      body: Container(
        padding: EdgeInsets.all(15),
        child: FutureBuilder<List<Section>>(
          future: sectionsProvider.getSections(),
          builder: (BuildContext context,AsyncSnapshot<List<Section>> snapshot){
            if(!snapshot.hasData){
              return Text('Loading...');
            }else{
              return DropdownButtonFormField<Section>(
                //decoration: inputDecoration.copyWith(hintText: currentSection.title),
                //value: currentSection,
                items: snapshot.data.map((section){
                  return DropdownMenuItem<Section>(
                    value: section,
                    child: Row(
                      children: [
                        Icon(
                          Icons.brightness_1,
                          size: 15,
                          color: Color(section.color),
                        ),
                        SizedBox(width: 20,),
                        Text(section.title),
                      ],
                    ),
                  );
                },
                ).toList(),
                isExpanded: false,
                isDense: true,
                onChanged: (value){
                  setState(() {
                    currentSection = value;
                  });
                },
              );
            }
          },
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: firebase flutter sqflite


    【解决方案1】:

    DropdownButtonFormField 缺少值参数,因为您收到此错误。

    以下行的注释对你有用。

    value: currentSection,
    

    更新:

    我认为问题在于您将整个对象分配给 value 参数,下拉列表必须将 value 与下拉列表值进行比较以检查是否有新的分配值在下拉项目列表中可用。

    然而,在flutter(dart)中,我们不能直接比较object。你必须重写 == 操作符和 hascode ,但我们可以使用 Equatable 包轻松比较。

    我不认识你的 Section 类,所以按照我在下面的示例类中所做的更改。

    首先,在 pubspec.yaml 文件中包含 Equatable 包。

    class Section extends Equatable {
      final int id;
      Section({this.id});
    
      @override
      List<Object> get props => [id];  // pass all variable with(,) separated as i pass id.
    }
    

    【讨论】:

    • 是的,当我删除该行上的评论时,整个屏幕会崩溃,并且会显示与仅显示红色错误屏幕相同的错误!
    • 还添加了 snapshot.data 输出。
    • 我已将错误截图包含在原帖中! snapshot.data 的输出是 Section 对象[Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section', Instance of 'Section'] 的列表
    • 添加 snapshot.data 输出。
    • 我已将其包含在我上次的回复中
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2022-01-12
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多