簡體   English   中英

顫振:輸入&#39;列表<dynamic> &#39; 不是 &#39;List 類型的子類型<DropdownMenuItem<String> &gt;&#39;

[英]flutter: type 'List<dynamic>' is not a subtype of type 'List<DropdownMenuItem<String>>'

在我的 flutter 應用程序中,我有一個下拉列表,下面是我將數據加載到其中的方式。

Consumer<ProductsImpl>(
                  builder: (context, data, child) {
                    print("consumer running");

                    return DropdownButton(
                        hint: Text(
                          "Please Select          ",
                          style: TextStyle(
                            fontSize: 14,
                          ),
                        ),
                        items: data.geProductAndTypeList.map((info) {
                          return DropdownMenuItem(
                            child: new Text(info,
                                style: TextStyle(
                                  fontSize: 14,
                                )),
                            value: data,
                          );
                        }).toList(),
                        onChanged: (String newValue) {
                          setState(() {
                            _productdropDown = newValue;
                            print(newValue);
                          });
                        },
                        value: _productdropDown);
                  },
                ),

下面是您可以找到getProductAndTypeList的代碼

class ProductsImpl with ChangeNotifier {
  NavLinks _navLinks = new NavLinks();

  List<FreshProducts> _freshProductList = [];

  List<String> _productAndTypeList = [];

  get geProductAndTypeList => _productAndTypeList;

  Future<void> geFreshProductsBySpecies(int id) async {
    try {
      var data = await http.get(_navLinks.getFreshProductsBySpecies(id));
      var jsonData = convert.json.decode(data.body).cast<Map<String, dynamic>>();
      _freshProductList = jsonData
          .map<FreshProducts>((json) => new FreshProducts.fromJson(json))
          .toList();

      print("Product sIZE: " + _freshProductList.length.toString());
    } catch (error) {
      throw error;
    }

    //notifyListeners();
  }

  Future<void> buildProductAndTypeList(int speciesID) async{

    print("asasas");

    for(int i=0; i < _freshProductList.length ; i++)
    {
      if(_freshProductList[i].productSpecies.idproductSpecies == speciesID)
      {
        String str = _freshProductList[i].productCategory.name + ", "+ _freshProductList[i].productType.type;

        if(!_productAndTypeList.contains(str))
        {
          _productAndTypeList.add(str);
          print(str);
        }
      }
    }

    notifyListeners();

  }
}

當下拉列表正在構建時,我最終會出現以下錯誤。

[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown building Consumer<ProductsImpl>(dirty, dependencies: [_DefaultInheritedProviderScope<ProductsImpl>]):[39;49m
type 'List<dynamic>' is not a subtype of type 'List<DropdownMenuItem<String>>'


[38;5;248mEither the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md
[39;49m

[38;5;244mThe relevant error-causing widget was[39;49m
    [38;5;248mConsumer<ProductsImpl>[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;248m#0      ProductUIState._buildForm.<anonymous closure>[39;49m
[38;5;248m#1      Consumer.buildWithChild[39;49m
[38;5;248m#2      SingleChildStatelessWidget.build[39;49m
[38;5;244m#3      StatelessElement.build[39;49m
[38;5;248m#4      SingleChildStatelessElement.build[39;49m
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

我該如何解決這個問題?

答案是使用指定的返回類型為_productAndTypeList創建自定義 get 方法。 get geProductAndTypeList => _productAndTypeList; 被使用,它正在返回一個動態。

我做了下面的方法。

List<String> geProductAndTypeList()
  {
    return _productAndTypeList;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM