当我使用 http.get() 从 API 获取数据时出现错误
i get errors when I fetch my data from an API using http.get()
我在我的代码中遇到了这个问题,但我无法解决它。我正在尝试通过 API 获取数据,但我在 IDE.
中遇到了这些错误
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
#0 Products.fetchAndSetProducts.<anonymous closure> (package:shop_app/providers/products_provider.dart:85:28)
#1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
#2 Products.fetchAndSetProducts (package:shop_app/providers/products_provider.dart:81:21) <asynchronous suspension>
以上错误指向此函数
Future<void> fetchAndSetProducts() async {
final url = Uri.parse(
'https://flutter-update-607c9-default-rtdb.firebaseio.com/products.json',
);
try {
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
final List<Product> loadedProducts = [];
extractedData.forEach((prodId, prodData) {
loadedProducts.add(
Product(
id: prodId,
title: prodData['title'],
description: prodData['description'],
price: prodData['price'],
isFavorite: prodData['isFavorite'],
imageUrl: prodData['imageUrl'],
),
);
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
rethrow;
}
}
这是我的产品class
class Product with ChangeNotifier {
final String? id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product(
{required this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false});
}
如有任何帮助,我们将不胜感激!
JSON at the URL 中的最后一个条目看起来与其他条目不同:.
{
"-MxpO80SMxlqxMs6Q2-O" : {
"description" : "is this long enough for you",
"imageUrl" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg",
"isFavorite" : true,
"price" : 420.99,
"title" : "pan"
},
"-MxxqC8KwLPlgsmWBITe" : {
"description" : "this is a test long one",
"imageUrl" : "https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg",
"isFavorite" : true,
"price" : 12.0,
"title" : "Yellow scarf"
},
"-MySBTTTKRTj4jXzAlhA" : {
"amount" : 12.0,
"dateTime" : "2022-03-18T15:22:46.740475",
"products" : [ {
"id" : "2022-03-18 15:22:43.791990",
"price" : 12.0,
"quantity" : 1,
"title" : "Yellow scarf"
} ]
}
}
虽然 Firebase 可以处理无模式数据,但您的代码希望每个 child 节点都具有某些属性,并且 -MySBTTTKRTj4jXzAlhA
节点没有 ID、标题、描述,imageUrl
,或您尝试阅读的任何其他属性。
我在我的代码中遇到了这个问题,但我无法解决它。我正在尝试通过 API 获取数据,但我在 IDE.
中遇到了这些错误 [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
#0 Products.fetchAndSetProducts.<anonymous closure> (package:shop_app/providers/products_provider.dart:85:28)
#1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
#2 Products.fetchAndSetProducts (package:shop_app/providers/products_provider.dart:81:21) <asynchronous suspension>
以上错误指向此函数
Future<void> fetchAndSetProducts() async {
final url = Uri.parse(
'https://flutter-update-607c9-default-rtdb.firebaseio.com/products.json',
);
try {
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
final List<Product> loadedProducts = [];
extractedData.forEach((prodId, prodData) {
loadedProducts.add(
Product(
id: prodId,
title: prodData['title'],
description: prodData['description'],
price: prodData['price'],
isFavorite: prodData['isFavorite'],
imageUrl: prodData['imageUrl'],
),
);
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
rethrow;
}
}
这是我的产品class
class Product with ChangeNotifier {
final String? id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product(
{required this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false});
}
如有任何帮助,我们将不胜感激!
JSON at the URL 中的最后一个条目看起来与其他条目不同:.
{
"-MxpO80SMxlqxMs6Q2-O" : {
"description" : "is this long enough for you",
"imageUrl" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg",
"isFavorite" : true,
"price" : 420.99,
"title" : "pan"
},
"-MxxqC8KwLPlgsmWBITe" : {
"description" : "this is a test long one",
"imageUrl" : "https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg",
"isFavorite" : true,
"price" : 12.0,
"title" : "Yellow scarf"
},
"-MySBTTTKRTj4jXzAlhA" : {
"amount" : 12.0,
"dateTime" : "2022-03-18T15:22:46.740475",
"products" : [ {
"id" : "2022-03-18 15:22:43.791990",
"price" : 12.0,
"quantity" : 1,
"title" : "Yellow scarf"
} ]
}
}
虽然 Firebase 可以处理无模式数据,但您的代码希望每个 child 节点都具有某些属性,并且 -MySBTTTKRTj4jXzAlhA
节点没有 ID、标题、描述,imageUrl
,或您尝试阅读的任何其他属性。