【问题标题】:Flutter Dropdown: type 'String' is not a subtype of type 'int' of 'index'Flutter Dropdown:类型'String'不是'index'类型'int'的子类型
【发布时间】:2020-10-02 23:09:00
【问题描述】:

我正在尝试在颤振中使用下拉列表,并且下拉列表的值应该由 HTTP 请求添加。

这是 HTTP 的代码。

Future<String> getData() async {
    try{ 
    var deviceid = '123';
      var dtgUid = '123';

      var body = { "uid" : dtgUid, "deviceid": deviceid};

       var response = await http.post(url, body: json.encode(body)).timeout(Duration(seconds: 15),
            onTimeout: (){
                    //  throw Exception();
              _showSnackBar(context,'Some issue with connectivity. Can not connect to server.',Colors.redAccent);
                      //or you can also
              return null;
            });

     if(response. statusCode == 200){
        final datafromdb = getNewsTypeFromJson(response.body);
        setState(() {
          _inProcess = false;
                        if(datafromdb.content.length == null){
                          count = 0;
                        }else{
                          count = datafromdb.content.length;
                        }

                         if(datafromdb.content.length > 0 && datafromdb.content[0].tagname != 'Empty'){
                        for (var i in datafromdb.content) {
                          print(i.tagname);
                          data.add(i.tagname);
                        }  
                      }else{
                        nodata = 'No Record Found';
                        _showSnackBar(context,nodata,Colors.redAccent);
                      }

        });
     }
       }catch(e){
       print("Exception Caught: $e");
    }


    }

这里是 JSONParse。

import 'dart:convert';

GetNewsType getNewsTypeFromJson(String str) => GetNewsType.fromJson(json.decode(str));

class GetNewsType {
    GetNewsType({
        this.content,
        this.success,
    });

    List<Content> content;
    bool success;

    factory GetNewsType.fromJson(Map<String, dynamic> json) => GetNewsType(
        content: List<Content>.from(json["content"].map((x) => Content.fromJson(x))),
        success: json["success"],
    );

}

class Content {
    Content({
        this.tagname,
    });

    String tagname;

    factory Content.fromJson(Map<String, dynamic> json) => Content(
       // tagname: json["tagname"],
        tagname: json == null ? 'Empty' : json["tagname"]
    );
}

这是下拉菜单的代码。

DropdownButton(
                        items: data.map((item) {
                          return DropdownMenuItem(
                            child: Text(item['tagname']),
                            value: item['tagname'].toString(),
                          );
                        }).toList(),
                        onChanged: (newVal) {
                          setState(() {
                            _mySelection = newVal;
                          });
                        },
                        value: _mySelection,
                      ),

当我运行应用程序时,它给出了以下错误。我不明白。

type 'String' is not a subtype of type 'int' of 'index'

【问题讨论】:

  • 使用 return new DropdownMenuItem(child: new Text(item.tagname), value: item.tagname, );
  • 谢谢。我这样做了,它解决了这个问题。

标签: flutter dart


【解决方案1】:

我还没有执行代码,但是错误消息和代码 item['tagname'] 中的这一行看起来就像 List&lt;String&gt; 正在被 key 访问,就像 map 一样。 因为在 .map() 中你有单独的 Content 对象使用 .item.tagname

【讨论】:

  • 感谢您的回答。我到底需要做什么来解决这个问题。抱歉,我的知识很少。
  • 使用item['tagname']的地方,改成item.tagname
猜你喜欢
  • 2020-04-22
  • 2021-05-10
  • 2019-04-24
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2021-08-10
  • 2021-09-08
相关资源
最近更新 更多