因此,我嘗試使用 API,并且不斷收到此錯誤型別 '(dynamic) => Category' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'
這是我的類別模型
import 'dart:convert';
List<Category> categoryFromJson(String str) =>
List<Category>.from(json.decode(str).map((x) => Category.fromJson(x))); // I get the error here
String categoryToJson(List<Category> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Category {
Category({
required this.id,
required this.name,
required this.icon,
});
int id;
String name;
String icon;
factory Category.fromJson(Map<String, dynamic> json) => Category(
id: json["id"],
name: json["name"],
icon: json["icon"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"icon": icon,
};
}
這是我的類別存盤庫。更像服務
import 'package:elibrary/services/requests.dart';
import 'package:elibrary/services/endpoints.dart';
import 'package:get/get.dart';
class CategoryRepository extends GetxService {
ApiClient apiClient = ApiClient();
Future<Response> getCategories() async {
return await apiClient.getRequest(ProjectConstants.CATEGORY_URI);
}
}
這是類別控制器
import 'dart:io';
import 'package:elibrary/model/categories.dart';
import 'package:elibrary/services/repository/category_repository.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class CategoryController extends GetxController {
CategoryRepository categoryRepository = CategoryRepository();
RxList<Category> categoryList = <Category>[].obs;
RxBool isLoading = false.obs;
@override
void onInit() {
super.onInit();
getCategoryList();
}
Future<void> getCategoryList() async {
isLoading(true);
try {
Response categoryResponse = await categoryRepository.getCategories();
if (categoryResponse.statusCode == 200) {
categoryList.assignAll(
categoryFromJson(categoryResponse.bodyString ?? ''),
);
} else {
debugPrint(categoryResponse.bodyString ?? '');
debugPrint(
categoryResponse.statusText.toString(),
);
}
} on SocketException {
GetSnackBar(
message: "No Internet Connectivity",
duration: Duration(seconds: 5),
).show();
} catch (e) {
debugPrint(
e.toString(),
);
} finally {
isLoading(false);
}
}
SnackbarController showErrorMessage(
Response<dynamic> categoryResponse, String message) {
return Get.snackbar(
"Error Occurred",
categoryResponse.statusText.toString(),
snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white,
backgroundColor: Colors.red,
);
}
}
我嘗試通過這種方式在回圈中使用
通過改變這個
List<Category> categoryFromJson(String str) => List<Category>.from(json.decode(str).map((x) => Category.fromJson(x)));
對此
List<Category> categoryFromJson(String str) => [
for (Map<String, dynamic> x in json.decode(str)) Category.fromJson(x),
];
但它給了我這個錯誤
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
我已閱讀此未處理的例外
我也讀過這個發生例外
我再次有這也顫抖_TypeError
uj5u.com熱心網友回復:
嘗試這個:
List<Category> categoryFromJson(String str) => (json.decode(str)["data"] as List<Map<String, dynamic>>).map((x) => Category.fromJson(x)).toList();
或者如果你想要 for 回圈格式:
List<Category> categoryFromJson(String str) => [
for (Map<String, dynamic> x in json.decode(str)["data"]) Category.fromJson(x),
];
uj5u.com熱心網友回復:
嘗試更改以下代碼行
List<Category> categoryFromJson(String str) => List<Category>.from(json.decode(str).map((x) => Category.fromJson(x)));
to(添加 toList() 到地圖閉包)
List<Category> categoryFromJson(String str) => List<Category>.from(json.decode(str).map((x) => Category.fromJson(x)).toList());
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531949.html
標籤:扑api镖
