当前位置: 首页 > 知识库问答 >
问题:

_TypeError(类型列表不是类型映射的子类型)颤振

隗高旻
2023-03-14

我有一个问题,因为2小时。我有这个错误,我看到了更多的主题,但我不能解决它。

消息:_TypeError(类型列表不是类型映射的子类型)颤动

我的模型:


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();
            },
          ),
        ),
    );
  }
}

我尝试了更多的教程和不同的主题,但我有相同的错误。。。

非常感谢。

共有1个答案

斜淳
2023-03-14

jsonDecode(字符串源)如果json如下所示,则返回一个列表:

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

并返回一个映射

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

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

 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中的所有主题转换为主题对象,你需要遍历列表并一个接一个地转换它们:

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');
  }
}

 类似资料:
  • 你好,我正在做一个api调用,在那里我得到了一系列的横幅。我正在犯错误- 横幅。飞奔 横幅。飞奔 api_base_助手。飞奔 得到你的横幅。飞奔

  • 我正面临着一个奇怪的颤动错误。我使用json序列化。 这是我的密码 我的web api发送的数据如下 这是数组的数组。 生成错误的代码是 它给出的错误 JSON数据这里是JSON数据格式的屏幕截图

  • 当我尝试解码json时,它会给我错误。 我的模范班 功能在我使用它的地方 json数据

  • 我对Flutter编程非常陌生,我正在尝试导入一个本地JSON文件,其中包含书籍数据,如标题、作者、发布年份等。 最初我使用JSON-to-DART转换器来组装一个图书数据模型,现在我正在尝试创建一个函数,在该函数中,程序获取本地JSON文件,并使用类中的方法将数据解析为地图。 我遇到的问题是返回类型

  • 我在这里做了一个类似的帖子:问题,但我做了同样的事情,我一直是问题... 我有我的班级: ask API的方法: 当我询问我的API时,请求的结果是: {结果:[{id:2,题为:语言利用率是多少?难度:1,答案:[{id:9,题为:Oui,isCorrect:true},{id:10,题为:Non,isCorrect:false}],页码:3,下一步:http://localhost:3000/

  • 我目前正在构建一个应用程序,通过医疗api读取数据,我试图解析JSON。 我为用户创建了一个模型类: 这是我试图阅读的JSON信息: 但是我得到的错误列表不是Map类型的子类型 如何修复此错误?