json - 未处理的异常 : type '_InternalLinkedHashMap

我正在尝试解析 json,将其打印到控制台,然后放入 ListView.builder 并收到此错误:type '_InternalLinkedHashMap' is not a subtype of type 'String'

我该如何解决?

列表数据

FutureBuilder(
  future: restaurantSearch,
  builder: (context, AsyncSnapshot<RestaurantSearch> snapshot) {
    if(snapshot.connectionState == ConnectionState.waiting) {
      return Expanded(child: Center(child: CircularProgressIndicator(strokeWidth: 3)));
    } else if(snapshot.connectionState == ConnectionState.done) {
      if(snapshot.hasData) {
        return ListView.builder(
          shrinkWrap: true,
          itemCount: snapshot.data.restaurants.length,
          itemBuilder: (context, index) {
            var restaurant = snapshot.data.restaurants[index];
            return ListTile(
              title:Text(restaurant.name),
            );
          }
        );
      }
    } else if (snapshot.hasError) {
        return Center(child: Text(snapshot.error.toString()));
    }
    return Text('');
  }
)

api服务

Future<RestaurantSearch> restaurantSearch(String query) async {
    final response = await dio.get("https://restaurant-api.dicoding.dev/search", queryParameters: {"q": query});
    print(response.data);
    if(response.statusCode == 200) {
      return RestaurantSearch.fromJson(json.decode(response.data));
    } else {
      throw Exception("Failed to Load Detail Restaurant, Please Check Your Internet");
    }
  }

模型

import 'dart:convert';

RestaurantSearch restaurantSearchFromJson(String str) => RestaurantSearch.fromJson(json.decode(str));

String restaurantSearchToJson(RestaurantSearch data) => json.encode(data.toJson());

class RestaurantSearch {
  RestaurantSearch({
    this.error,
    this.founded,
    this.restaurants,
  });

  bool error;
  int founded;
  List<RestaurantS> restaurants;

  factory RestaurantSearch.fromJson(Map<String, dynamic> json) => RestaurantSearch(
    error: json["error"],
    founded: json["founded"],
    restaurants: List<RestaurantS>.from(json["restaurants"].map((x) => RestaurantS.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "error": error,
    "founded": founded,
    "restaurants": List<String>.from(restaurants.map((x) => x.toJson())),
  };
}

class RestaurantS {
  RestaurantS({
    this.id,
    this.name,
    this.description,
    this.pictureId,
    this.city,
    this.rating,
  });

  String id;
  String name;
  String description;
  String pictureId;
  String city;
  double rating;

  factory RestaurantS.fromJson(Map<String, dynamic> json) => RestaurantS(
    id: json["id"],
    name: json["name"],
    description: json["description"],
    pictureId: json["pictureId"],
    city: json["city"],
    rating: json["rating"].toDouble(),
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "name": name,
    "description": description,
    "pictureId": pictureId,
    "city": city,
    "rating": rating,
  };
}

非常感谢您的回答................................................ ..................................................... ..................................................... ..................................................... ...................................................

最佳答案

你需要删除 json.decode 因为 Dio 已经为你解码了正文

改变这一行

return RestaurantSearch.fromJson(response.data);

关于json - 未处理的异常 : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66501665/

相关文章:

python - 在 Python 中获取 Windows 版本

python - 如何使用函数跳出 for 循环?

c++ - 在 C 和 C++ 中, "#"是什么意思?

awk - 如何在 Awk 中使用单个正则表达式提取多个字符串

list - 理解实现 foldr 和 foldl 的函数

database - 使用golang从postgres中的csv批量插入而不使用for循环

python - python中列表列表之间的叉积

javascript - typescript :为什么我们不能为泛型类型分配默认值?

flutter - 为什么我们应该在 dart 中使用 static 关键字代替抽象?

c++ - 基本 ODR 违规 : member functions in . h 文件