常量错误-类型列表动态不是类型映射字符串动态的子类型



我正在从API中消费数据,每当我尝试这样做时,我总是得到这个错误。类型列表动态不是类型映射字符串动态的子类型

在我试图寻找答案时,我遇到了这个类似的问题

我也经历过这个另一个类似的问题

这也是一个类似的问题

从这个类似的问题,我意识到似乎有一个数据结构不匹配,但我似乎不能得到它的解决方案。

下面是我的代码节选

这是对象模型


class Book {
int? id = 0;
String? title = "";
String? description = "";
String? image = "";
String? author = "";
Book({
this.id,
this.title,
this.description,
this.image,
this.author,
});
Book.fromJson(Map<String, dynamic> parsedJson) {
id = parsedJson['id'];
title = parsedJson['title'];
description = parsedJson['description'];
image = parsedJson['image'];
author = parsedJson['author'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
data['description'] = this.description;
data['image'] = this.image;
data['author'] = this.author;
return data;
}
}

这是包含错误的控制器类。我能够打印来自后端的内容。


import 'dart:io';
import 'package:elibrary/model/books.dart';
import 'package:elibrary/services/repository/book_repository.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class BookController extends GetxController {
BookRepository bookRepository = BookRepository();
RxBool isLoading = false.obs;
RxList<Book> bookList = <Book>[].obs;
@override
void onInit() {
super.onInit();
fetchBooksList();
}
Future<void> fetchBooksList() async {
isLoading(true);
try {
Response bookResponse = await bookRepository.fetchBooks();
if (bookResponse.statusCode == 200) {

for (var element in bookResponse.body) {
bookList.add(Book.fromJson(element));
}

} else {
Get.snackbar(
'Error Occurred',
bookResponse.statusText.toString(),
snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white,
backgroundColor: Colors.red,
);
}
} catch (e) {
debugPrint(
e.toString(),
);
Get.snackbar(
"Error Occurred",
e.toString(),
snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white,
backgroundColor: Colors.green,
duration: Duration(seconds: 5),
).show();
} finally {
isLoading(false);
}
}
}

我确实尝试将模型对象更改为这个


import 'dart:convert';
Book bookFromJson(String str) => Book.fromJson(json.decode(str));
String bookToJson(Book data) => json.encode(data.toJson());
class Book {
Book({
this.id,
this.title,
this.description,
this.image,
this.author,
});
int id;
String title;
String description;
String image;
String author;
factory Book.fromJson(Map<String, dynamic> json) => Book(
id: json["id"],
title: json["title"],
description: json["description"],
image: json["image"],
author: json["author"],,
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"description": description,
"image": image,
"author": author,
};
}

然后我也试过这样操作控制器


import 'dart:io';
import 'package:elibrary/model/books.dart';
import 'package:elibrary/services/repository/book_repository.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class BookController extends GetxController {
BookRepository bookRepository = BookRepository();
RxBool isLoading = false.obs;
RxList<Book> bookList = <Book>[].obs;
@override
void onInit() {
super.onInit();
fetchBooksList();
}
Future<void> fetchBooksList() async {
isLoading(true);
try {
Response bookResponse = await bookRepository.fetchBooks();
if (bookResponse.statusCode == 200) {
bookList.assAll( 
bookFromJson(bookResponse.bodyString ?? ''),
)
} else { 
Get.snackbar(
'Error Occurred',
bookResponse.statusText.toString(),
snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white,
backgroundColor: Colors.red,
);
}
} catch (e) {
debugPrint(
e.toString(),
);
Get.snackbar(
"Error Occurred",
e.toString(),
snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white,
backgroundColor: Colors.green,
duration: Duration(seconds: 5),
).show();
} finally {
isLoading(false);
}
}
}
```
```

Again I tried decoding the response this way 

```
```
var jsonList = jsonDecode(bookResponse.bodyString ?? '')
.map((book) => Book.fromJson(book))
.toList();
bookList.assignAll(jsonList);
debugPrint('Total Book List is: ${bookList.length}');

所有这些尝试都会产生相同的错误。

这些是API响应

I/flutter ( 5788): key = data, value = [{id: 1, name: dolore, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/007766?text=architecto}, {id: 2, name: repellat, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/004444?text=voluptatum}, {id: 3, name: est, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/005577?text=et}, {id: 4, name: quasi, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/00cc00?text=deserunt}, {id: 5, name: provident, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/008888?text=et}, {id: 6, name: quo, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/007777?text=dolorem}, {id: 7, name: expedita, icon: http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/00aa88?text=adipisci}, {id: 8, name: quia, icon: http://192.168.1.102:8000/images/categor
I/flutter ( 5788): result = {"data":[{"id":1,"name":"dolore","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/007766?text=architecto"},{"id":2,"name":"repellat","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/004444?text=voluptatum"},{"id":3,"name":"est","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/005577?text=et"},{"id":4,"name":"quasi","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/00cc00?text=deserunt"},{"id":5,"name":"provident","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/008888?text=et"},{"id":6,"name":"quo","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/007777?text=dolorem"},{"id":7,"name":"expedita","icon":"http://192.168.1.102:8000/images/categories/https://via.placeholder.com/640x480.png/0
I/flutter ( 5788): Total Book List is: 0
I/flutter ( 5788): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

您的响应是map,您需要在其上使用["data"]来获得您想要的列表。所以在Book类中修改这个:

Book bookFromJson(String str) => Book.fromJson(json.decode(str));

:

List<Book> bookFromJson(String str) => (json.decode(str)["data"] as List).map((e) => Book.fromJson(e)).toList();

您正在接收JSON列表每一项都要传递给Book.parseJson函数。

List<Book> bookFromJson(String str) {
Iterable jsonArray = json.decode(str);
return List<Book>.from(jsonArray.map((json) => Book.fromJson(json)));
}

相关内容

  • 没有找到相关文章

最新更新