flutter - Flutter下拉列表: 'String'类型不是 'int'的 'index'类型的子类型

标签 flutter dart

我试图在flutter中使用下拉列表,并且应该通过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'

最佳答案

我还没有执行代码,但是错误消息和代码item['tagname']中的这一行看起来像是像List<String>这样的key正在访问map
由于在.map()中,您拥有单独的Content对象,因此请像.一样使用item.tagname

关于flutter - Flutter下拉列表: 'String'类型不是 'int'的 'index'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62356026/

相关文章:

flutter - 在 CustomScrollView 中允许一点过度滚动的最佳方法

firebase - Flutter Cloud Firestore编译错误: CloudFirestorePlugin.java uses unchecked or unsafe operations

flutter - 如何连接两个 TextStyle?

dart - Dart 类中带下划线的命名参数

twitter-bootstrap - 无法读取 'package:rikulo_commons/html.dart'(找不到 Assets rikulo_commons | lib/html.dart。)

flutter - 清除 flutter 中的文本字段时出现错误 "getter ' 年'被调用为空"

ios - 为 Flutter App 自定义 BottomNavigationBar selected BottomNavigationBarItem

firebase - 如何访问 firebase 身份验证部分

flutter - 基于用户输入的动态 TextField 宽度

Flutter:如何在textformfield上没有输入时隐藏后缀图标?