class Phone{
int? id;
bool? is_new;
String? title;
String? subtitle;
String? picture;
bool? is_buy;
dynamic best_seller;
int? price_without_discount;
int? discount_price;
Phone({
this.id,
this.is_new,
this.title,
this.subtitle,
this.picture,
this.is_buy,
this.best_seller,
this.price_without_discount,
this.discount_price
});
factory Phone.fromJson(Map <String, dynamic> json) => Phone(
id: json[MyPhoneKeys.id],
is_new: json[MyPhoneKeys.is_new],
title: json[MyPhoneKeys.title],
subtitle: json[MyPhoneKeys.subtitle],
picture: json[MyPhoneKeys.picture],
is_buy: json[MyPhoneKeys.is_buy],
best_seller: json[MyPhoneKeys.best_seller],
price_without_discount: json[MyPhoneKeys.price_without_discount],
discount_price: json[MyPhoneKeys.discount_price]
);
}
(我的螢屏顯示資料)
class CarouselSliderData extends StatefulWidget{
const CarouselSliderData({super.key});
@override
State<CarouselSliderData> createState() => CarouselSliderDataState();
}
class CarouselSliderDataState extends State<CarouselSliderData> {
int? id;
bool? is_new;
String? title;
String? subtitle;
dynamic picture;
bool? is_buy;
dynamic best_seller;
int? price_without_discount;
int? discount_price;
late Future<dynamic> phoneSpec;
@override
void initState() {
phoneSpec = MyApiService().getDataMocky();
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<dynamic>(
future: phoneSpec,
builder: (context, snapshot) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(snapshot.data!.id),
Text(snapshot.data!.is_new),
Text(snapshot.data!.title),
Text(snapshot.data!.subtitle),
Text(snapshot.data!.picture),
Text(snapshot.data!.is_buy),
Text(snapshot.data!.best_seller),
Text(snapshot.data!.price_without_discount),
Text(snapshot.data!.discount_price),
],
);
}
);
}
Future<dynamic> getData() async{
return await MyApiService().getDataMocky().then((value) async{
if(value != null){
setState((){
id = value!.id!;
is_new = value!.is_new!;
title = value!.title!;
subtitle = value!.subtitle!;
picture = value!.picture!;
is_buy = value!.is_buy!;
best_seller = value!.best_seller!;
price_without_discount = value!.price_without_discount!;
discount_price = value!.discount_price!;
});
return value;
}
},
);/*.catchError((_){
throw Exception("Exception is caught");
});*/
}
}
(我獲取資料的服務)
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:my_work/apiService/model.dart';
class MyApiService{
Future<dynamic> getDataMocky() async {
final response = await http.get(
Uri.parse('https://run.mocky.io/v3/654bd15e-b121-49ba-a588-960956b15175')
);
if(response.statusCode == 200){
return Phone.fromJson(json.decode(response.body)[1]);
}
return Exception();
}
}
我的模型是否適合來自這個 api的這個 api json 。我想獲取資料并將它們顯示在輪播滑塊中(我將添加它)但得到空值。錯誤在說Null is not subtype of String and nullcheck is used on NULL value,我不知道為什么以及我的錯誤在哪里。非常感謝
uj5u.com熱心網友回復:
Text小部件不接受可為空的字串。
與其使用!它,不如先檢查 null,或者您可以在 null 情況下提供默認值。格式會像
if(snapshot.data!=null) Text(snapshot.data.id),
或者
Text(snapshot.data?.id??"got null"),
或者使用像下面這樣的字串格式,它會在 null 情況下顯示 null。
Text("${snapshot.data?.id}"),
我會建議檢查理解零安全性
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515397.html
上一篇:在顫動中無法捕獲dio包的錯誤
下一篇:在帶有標題的熊貓資料框中寫入變數
