【问题标题】:Flutter app with loading while retrieving API在检索 API 时加载的 Flutter 应用程序
【发布时间】:2020-11-18 12:39:16
【问题描述】:

我正在使用返回这样的值的 API。

[
    {
        "store": "AMAZON"
    },
    {
        "store": "FLIPKART"
    },
    {
        "store": "WALMART"
    },
    {
        "store": "ALIBABA"
    },

]

我需要这个在下拉列表中。 我需要一个包含此 API 数据的下拉按钮。请有人帮忙。我尝试了很多方法,但没有任何效果。

【问题讨论】:

  • 你尝试过哪种方式?请输入代码以获得帮助!
  • @Hakiz'a 我添加了上一个问题的链接stackoverflow.com/questions/64888506/… 请帮助我。从早上起,什么都没有解决。我很生气

标签: flutter dart


【解决方案1】:

nigale试试代码:

List<String> markets = []; // Or  var markets = [];

  String _mySelection;
  @override
  void initState() {
    buidDropDownItems();
    super.initState();
  }

// 
void buidDropDownItems() async {
    markets = await retrievedata.getMarket();
    // Refresh the UI if the State object has been created
    if(mounted){
      setState(() {});
    }
}

 child: DropdownButton(
          items: markets.map<DropdownMenuItem<String>>((String val){
                  return DropdownMenuItem<String>(value: val, child: Text(val));
                }).toList(), // Get items from the available data in markets variable
          onChanged: (sto) {
            setState(() {
              _mySelection = sto;
            });
          },
          value: _mySelection,
          hint: Text('Please select the store: '),
        ),

函数retrievedata.getMarket(); 返回Future&lt;dynamic&gt;dynamic 是您的市场列表。

【讨论】:

  • 它会导致另一个错误。 ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following NoSuchMethodError was thrown building homePage(dirty, state: homePageState#f375b): The method 'map' was called on null. Receiver: null Tried calling: map&lt;DropdownMenuItem&lt;String&gt;&gt;(Closure: (String) =&gt; DropdownMenuItem&lt;String&gt;) The relevant error-causing widget was: homePage
  • 确保这样做List&lt;String&gt; markets = []; // Or var markets = [];初始化市场列表
  • 那么就会出现这个错误。 type '(String) =&gt; DropdownMenuItem&lt;String&gt;' is not a subtype of type '(dynamic) =&gt; DropdownMenuItem&lt;String&gt;' of 'f' 就像早上一样。请单独解决。
  • 经过一些修改。现在产生了新的错误。 There should be exactly one item with [DropdownButton]'s value: . Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem&lt;T&gt; item) { return item.value == value; }).length == 1' The relevant error-causing widget was: homePage
  • 在这里查看我的工作示例codeshare.io/GbZB3b
【解决方案2】:

假设这个数组是在一个名为data的变量中检索的

 List<String>list=[];
for(var itemData in data){
  list.add(itemData['store']);
}

还有 wala,现在 list String 的 conation 数组可以使用了

【讨论】:

  • 请查看我添加了上一个问题的链接的评论。
  • 当我尝试 API 在 UI 渲染后返回。请给我一些解决方案。
猜你喜欢
  • 2018-10-02
  • 2020-04-19
  • 2016-03-19
  • 2020-08-28
  • 2020-11-22
  • 1970-01-01
  • 2023-01-05
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多