TypeError (type 'int' is not a subtype of type 'double') flutter
TypeError (type 'int' is not a subtype of type 'double') flutter
我正在尝试执行 http.get 请求以获取我已经发布(完美运行)到 firebase(实时存储)的数据,但是当我从未调用获取数据的方法时,它会抛出错误_TypeError(类型'int'不是类型'double'的子类型)请注意,我正在使用提供者状态管理
下面是用来fecth我的数据的方法
Future<void> getAndSetProducts() async {
const url = 'https://shop-12901-default-rtdb.firebaseio.com/products.json';
try {
final response = await http.get(Uri.parse(url));
var extractedResponse =
json.decode(response.body) as Map<String, dynamic>;
List<Product> loadedProducts = [];
extractedResponse.forEach((prodId, product) {
loadedProducts.add(
Product(
id: prodId,
title: product['title'],
price: product['price'], //-Sure the error is from here but not sure of how to resolve it-
imageUrl: product['imageUrl'],
description: product['description'],
isFavorite: product['isFavorite'],
),
);
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
throw error; //--------TypeError (type 'int' is not a subtype of type 'double')-------
}
}
下面也是我调用上面的方法来执行其任务的方法
@override
void didChangeDependencies() {
if (_isInit) {
setState(() {
isLoading = true;
});
try {
Provider.of<Products>(context).getAndSetProducts().then((_) {
setState(() {
isLoading = false;
});
});
} catch (error) {
print(error);
}
}
_isInit = false;
super.didChangeDependencies();
}
您可以在代码后使用 .toDouble()
函数(示例:-> product['price'].toDouble
)或者您可以在代码后使用 double.parse(value)
函数(示例:-> double.parse(product['price'])
)
尝试以 num
形式获取价格,然后将其解析为 double
。
Future<void> getAndSetProducts() async {
const url = 'https://shop-12901-default-rtdb.firebaseio.com/products.json';
try {
final response = await http.get(Uri.parse(url));
var extractedResponse =
json.decode(response.body) as Map<String, dynamic>;
List<Product> loadedProducts = [];
extractedResponse.forEach((prodId, product) {
loadedProducts.add(
Product(
id: prodId,
title: product['title'],
price: (product['price'] as num).toDouble(), //Try this
imageUrl: product['imageUrl'],
description: product['description'],
isFavorite: product['isFavorite'],
),
);
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
throw error; //--------TypeError (type 'int' is not a subtype of type 'double')-------
}
}
我正在尝试执行 http.get 请求以获取我已经发布(完美运行)到 firebase(实时存储)的数据,但是当我从未调用获取数据的方法时,它会抛出错误_TypeError(类型'int'不是类型'double'的子类型)请注意,我正在使用提供者状态管理
下面是用来fecth我的数据的方法
Future<void> getAndSetProducts() async {
const url = 'https://shop-12901-default-rtdb.firebaseio.com/products.json';
try {
final response = await http.get(Uri.parse(url));
var extractedResponse =
json.decode(response.body) as Map<String, dynamic>;
List<Product> loadedProducts = [];
extractedResponse.forEach((prodId, product) {
loadedProducts.add(
Product(
id: prodId,
title: product['title'],
price: product['price'], //-Sure the error is from here but not sure of how to resolve it-
imageUrl: product['imageUrl'],
description: product['description'],
isFavorite: product['isFavorite'],
),
);
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
throw error; //--------TypeError (type 'int' is not a subtype of type 'double')-------
}
}
下面也是我调用上面的方法来执行其任务的方法
@override
void didChangeDependencies() {
if (_isInit) {
setState(() {
isLoading = true;
});
try {
Provider.of<Products>(context).getAndSetProducts().then((_) {
setState(() {
isLoading = false;
});
});
} catch (error) {
print(error);
}
}
_isInit = false;
super.didChangeDependencies();
}
您可以在代码后使用 .toDouble()
函数(示例:-> product['price'].toDouble
)或者您可以在代码后使用 double.parse(value)
函数(示例:-> double.parse(product['price'])
)
尝试以 num
形式获取价格,然后将其解析为 double
。
Future<void> getAndSetProducts() async {
const url = 'https://shop-12901-default-rtdb.firebaseio.com/products.json';
try {
final response = await http.get(Uri.parse(url));
var extractedResponse =
json.decode(response.body) as Map<String, dynamic>;
List<Product> loadedProducts = [];
extractedResponse.forEach((prodId, product) {
loadedProducts.add(
Product(
id: prodId,
title: product['title'],
price: (product['price'] as num).toDouble(), //Try this
imageUrl: product['imageUrl'],
description: product['description'],
isFavorite: product['isFavorite'],
),
);
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
throw error; //--------TypeError (type 'int' is not a subtype of type 'double')-------
}
}