尝试在 Flutter 应用程序中对字符串进行 jsonDecode 时出错

Error on trying to jsonDecode String in Flutter application

当我尝试使用以下函数解码 json 字符串:[{"device-mac":"C9BF2EB47C17","on":true}] 时出现错误:

I/flutter ( 8954): type 'List' is not a subtype of type 'String'

被抛出。

我的函数是这个,json 字符串是从 Rest API 端点加载的:

Future<void> _getDevices() async {
    var parsedList;
    try {
      final response = await restApiClient.get(
        '/getDevices',
      );
      List<dynamic> parsedListJson = jsonDecode(response.data);
      List<Device> itemsList =
          List<Device>.from(parsedListJson.map((i) => Device.fromJson(i))); 
    
    }
  }

我认为它只能解析 Sting 并且你传递了一个映射列表,你应该制作一个循环然后使用 'response.data.toString' 将映射转换为字符串然后将值添加到列表中或者如果它是总是一个值尝试使用 'response.data.first.toString()

最终它不起作用的原因是我的 REST 端点已经返回了一个列表 JSON 类型,这使得函数“jsonDecode”变得多余。

我认为它只能解析 Sting,如果你传递了一个映射列表,你应该做一个循环,然后使用 'response.data.torturing 将映射转换为字符串,然后将值添加到列表中,或者如果它始终是一个值,请尝试使用 'response.data.first.toString()