简体   繁体   English

从 Hive 列表 (Flutter & Dart) 创建一组 DropdowMenuItems

[英]Create set of DropdowMenuItems from Hive List (Flutter & Dart)

I am trying to create this:我正在尝试创建这个:

return DropdownButton(
    value: selectedValue,
    items: dropdownItems
);

By getting data from a Hive Box and looping through it to create the DropdownMenuItem but keep hitting errors, this is my loop code:通过从 Hive Box 获取数据并循环遍历它以创建DropdownMenuItem但不断遇到错误,这是我的循环代码:

  List<LiveProject> lists = [];

  void getProjects() async {
    final box = await Hive.openBox<LiveProject>('project');
    setState(() {
      lists = box.values.toList();
    });
    List<DropdownMenuItem<dynamic>> menuItems = [
      for (var p in lists) {
        DropdownMenuItem(child: Text(p.title.toString()),value: p.id.toString()),
      }
    ];
    return menuItems;
  }

This is the error I am receiving:这是我收到的错误:

Error: A value of type 'List<DropdownMenuItem<dynamic>>' can't be returned from an async function with return type 'void'.
 - 'List' is from 'dart:core'.
 - 'DropdownMenuItem' is from 'package:flutter/src/material/dropdown.dart' ('../../flutter/packages/flutter/lib/src/material/dropdown.dart').
    return menuItems;
           ^

LiveProject is my Model and I have a List declared in there the dat being insert is from JSON and is an array of objects. LiveProject 是我的模型,我在其中声明了一个列表,其中插入的数据来自 JSON,并且是一个对象数组。

What am I doing wrong?我究竟做错了什么?

Instead of代替

void getProjects() 

do

List<DropdownMenuItem<dynamic>> getProjects() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM