我正在嘗試從 JSON 獲取雙精度值,但出現此錯誤。請幫助我如何解決這個問題。我不知道我在哪里搞砸了。
以下是我的錯誤:
AsyncSnapshot<List>(ConnectionState.done, null, type 'String' is not a subtype of type 'double', #0 new Product.fromMap (package:purple_star/screens/Model/product_model.dart:48:18) fetchProduct。 (package:purple_star/screens/Services/product_services.dart:13:50) MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31) ListIterator.moveNext (dart:_internal/iterable.dart:342:26) new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27) new _GrowableList.of (dart:core-patch/growable_array.dart:150:28) new List.of (dart:core-patch/array_patch。 dart:50:28) ListIterable.toList (dart:_internal/iterable.dart:213:44) fetchProduct (package:purple_star/screens/Services/product_services.dart:13:65)
class Product {
Product({
required this.productId,
required this.title,
required this.made,
required this.productImageUrl,
required this.strains,
required this.price,
required this.productType,
});
final int productId;
final String title;
final String made;
final String productImageUrl;
final String strains;
final double price;
ProductType productType;
factory Product.fromMap(Map<String, dynamic> json)
=> Product(
productId: json["productId"] as int,
title: json["title"] as String,
made: json["made"] as String,
productImageUrl: json["productImageUrl"] as String,
strains: json["strains"] as String,
price: json["price"] as double,
productType: ProductType.fromJson(json["productType"]));
}
Future<List<Product>> fetchProduct() async {
var URL = Uri.parse('https://mocki.io/v1/74d9dc9d-e33a-4fd3-b358-328d07be6aed');
final response = await http.get(URL);
if (response.statusCode == 200) {
final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
print(parsed);
return parsed.map<Product>((json) => Product.fromMap(json)).toList();
} else {
throw Exception('Failed to load Products');
}
}
提前致謝。
uj5u.com熱心網友回復:
試試下面的代碼希望對你有幫助。
double.tryParse(json['price']),
或嘗試以下答案
您的 API 呼叫:
Future<List<dynamic>> getInfoData() async {
String url = 'https://mocki.io/v1/74d9dc9d-e33a-4fd3-b358-328d07be6aed';
var response = await http.get(Uri.parse(url), headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
});
return json.decode(response.body);
}
你的小部件:
Expanded(
child: FutureBuilder<List<dynamic>>(
future: getInfoData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
var name = snapshot.data![index]['title'];
var price = snapshot.data![index]['price'];
var productImageUrl =
snapshot.data![index]['productImageUrl'];
var made = snapshot.data![index]['made'];
var type1 =
snapshot.data![index]['productType']['type1'];
var type2 =
snapshot.data![index]['productType']['type2'];
var type3 =
snapshot.data![index]['productType']['type3'];
return Card(
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.green.shade300),
borderRadius: BorderRadius.circular(15.0),
),
child: ListTile(
title: Text(name),
subtitle: Text(
made '\n' type1 '\n' type2 '\n' type3,
),
leading: Image.network(productImageUrl),
trailing: Text(price.toString()),
),
);
},
),
);
}
return Center(
child: const CircularProgressIndicator(),
);
},
),
),
您的結果螢屏-> 
uj5u.com熱心網友回復:
在您的 JSON 檔案中,“價格”的型別String不是double. 嘗試替換price: json["price"] as double為 double.tryParse(json["price"])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402065.html
