_TypeError(类型'List< dynamic>'不是类型'Map< String,dynamic>'的子类型) [英] _TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>') flutter

查看:202
本文介绍了_TypeError(类型'List< dynamic>'不是类型'Map< String,dynamic>'的子类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2个小时以来我有问题. 我遇到此错误,我看到了更多主题,但无法解决.

I have a problem since 2 hours. I have this error, i saw more topics but i can't solve it.

消息:_TypeError(类型列表"不是类型地图"的子类型)扑通

The message : _TypeError (type 'List' is not a subtype of type 'Map') flutter

我的模特:


class Theme {

  int id;
  String name;

  Theme({this.id, this.name});

  factory Theme.fromJson(Map<String, dynamic> json) {
    return Theme(
      id: json['id'],
      name: json['name'],
    );
  }

  Future<Theme> getThemes() async {
    String url = 'http://10.0.2.2:3000/v1/api/theme';
    final response =
        await http.get(url, headers: {"Accept": "application/json"});


    if (response.statusCode == 200) {
      return Theme.fromJson(json.decode(response.body));
    } else {
      throw Exception('Failed to load themes');
    }
  }

}

我的主题屏幕:


class _Theme extends State<Theme> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Blackbox"),
        ),
        body: Center(
          child: FutureBuilder<t.Theme>(
            future: t.Theme().getThemes(), //sets the getQuote method as the expected Future
            builder: (context, snapshot) {
              if (snapshot.hasData) { 
                new ListView.builder(
                  itemCount: _lengthList,
                  itemBuilder: (BuildContext context, int index) {
                    return Container(
                      child: new Text('${snapshot.data.name}'),
                    );
                  },
                );//checks if the response returns valid data              
              } else if (snapshot.hasError) { //checks if the response throws an error
                return Text("${snapshot.error}");
              }
              return CircularProgressIndicator();
            },
          ),
        ),
    );
  }
}

我尝试了更多的教程和不同的主题,但是我遇到了相同的错误...

I tried more tutorials and differents topics but i have the same error...

谢谢!

推荐答案

jsonDecode(String source)如果json是这样的,则返回一个列表:

jsonDecode(String source) return a List if the json is like this:

[{"id": 1,"name":"test theme"}]

,如果json看起来像这样,则返回Map<String,dynamic>:

and returns a Map<String,dynamic> if the json looks like this:

{"id": 1,"name":"test theme"}

如果要使用第一个主题,则应执行以下操作:

if you want to use the first theme you should do this:

 if (response.statusCode == 200) {
      return Theme.fromJson(json.decode(response.body)[0]); // this will return the first theme map
    } else {
      throw Exception('Failed to load themes');
    }

,如果您要将json中的所有主题转换为主题对象,则需要遍历列表并将它们一次一转换:

and if you want to convert all the themes in the json to Theme objects you need to go over the list and convert them one by one:

Future<List<Theme>> getThemes() async {
  String url = 'http://10.0.2.2:3000/v1/api/theme';
  final response = await get(url, headers: {"Accept": "application/json"});

  if (response.statusCode == 200) {
    List themesList = jsonDecode(response.body);
    List<Theme> themes = [];
    for(var themeMap in themesList){
      themes.add(Theme.fromJson(themeMap));
    }
    return themes;
  } else {
    throw Exception('Failed to load themes');
  }
}

这篇关于_TypeError(类型'List&lt; dynamic&gt;'不是类型'Map&lt; String,dynamic&gt;'的子类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆