Android _TypeError(type';List<;dynamic>;';不是type';Map<;String,dynamic>;';的子类型)flatter

Android _TypeError(type';List<;dynamic>;';不是type';Map<;String,dynamic>;';的子类型)flatter,android,flutter,flutter-layout,Android,Flutter,Flutter Layout,我有一个问题,因为2小时。 我有这个错误,我看到了更多的主题,但我不能解决它 消息:_TypeError(类型“List”不是类型“Map”的子类型)flatter 我的模型: class Theme { int id; String name; Theme({this.id, this.name}); factory Theme.fromJson(Map<String, dynamic> json) { return Theme( id:

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

消息:_TypeError(类型“List”不是类型“Map”的子类型)flatter

我的模型:


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

}


课堂主题{
int-id;
字符串名;
主题({this.id,this.name});
工厂主题.fromJson(映射json){
返回主题(
id:json['id'],
名称:json['name'],
);
}
Future getThemes()异步{
字符串url='0http://10.0.2.2:3000/v1/api/theme';
最后答复=
等待http.get(url,头:{“Accept”:“application/json”});
如果(response.statusCode==200){
返回Theme.fromJson(json.decode(response.body));
}否则{
抛出异常(“加载主题失败”);
}
}
}
我的主题屏幕:


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

类主题扩展了状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“黑盒”),
),
正文:中(
孩子:未来建设者(
future:t.Theme().getThemes(),//将getQuote方法设置为预期的未来
生成器:(上下文,快照){
if(snapshot.hasData){
新建ListView.builder(
itemCount:_lengthList,
itemBuilder:(构建上下文,int索引){
返回容器(
子项:新文本(“${snapshot.data.name}”),
);
},
);//检查响应是否返回有效数据
}else if(snapshot.hasError){//检查响应是否引发错误
返回文本(“${snapshot.error}”);
}
返回循环ProgressIndicator();
},
),
),
);
}
}
我尝试了更多的教程和不同的主题,但我有相同的错误

谢谢大家!

jsonDecode(字符串源)
如果json是这样的,则返回一个列表:

[{"id": 1,"name":"test theme"}]
{"id": 1,"name":"test theme"}
如果json如下所示,则返回一个
Map

[{"id": 1,"name":"test theme"}]
{"id": 1,"name":"test theme"}
如果要使用第一个主题,应执行以下操作:

if(response.statusCode==200){
return Theme.fromJson(json.decode(response.body)[0]);//这将返回第一个主题映射
}否则{
抛出异常(“加载主题失败”);
}
如果要将json中的所有主题转换为主题对象,则需要查看列表并逐个转换:

Future getThemes()异步{
字符串url='0http://10.0.2.2:3000/v1/api/theme';
最终响应=等待获取(url,标题:{“接受”:“应用程序/json”});
如果(response.statusCode==200){
List themesList=jsonDecode(response.body);
列出主题=[];
for(主题列表中的var主题映射){
themes.add(Theme.fromJson(themeMap));
}
返回主题;
}否则{
抛出异常(“加载主题失败”);
}
}
jsonDecode(字符串源)
如果json如下所示,则返回一个列表:

[{"id": 1,"name":"test theme"}]
{"id": 1,"name":"test theme"}
如果json如下所示,则返回一个
Map

[{"id": 1,"name":"test theme"}]
{"id": 1,"name":"test theme"}
如果要使用第一个主题,应执行以下操作:

if(response.statusCode==200){
return Theme.fromJson(json.decode(response.body)[0]);//这将返回第一个主题映射
}否则{
抛出异常(“加载主题失败”);
}
如果要将json中的所有主题转换为主题对象,则需要查看列表并逐个转换:

Future getThemes()异步{
字符串url='0http://10.0.2.2:3000/v1/api/theme';
最终响应=等待获取(url,标题:{“接受”:“应用程序/json”});
如果(response.statusCode==200){
List themesList=jsonDecode(response.body);
列出主题=[];
for(主题列表中的var主题映射){
themes.add(Theme.fromJson(themeMap));
}
返回主题;
}否则{
抛出异常(“加载主题失败”);
}
}

你能发布你的json响应吗?你能发布你的json响应吗?问你@aligator,我明天会试试,但多亏了你的解释,我明白了!:)谢谢@aligator,我明天会试试,但多亏了你的解释,我明白了!:)