我有json的模型,服务来获取api我只是不知道如何获得这样的图像从这个json从这个Ipone Mega是旋转木马滑块(json链接中有4个图像(,另一个下面只是列如果你能在旋转木马Slider上展示它,我将非常感谢你
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_work/apiService/fetch_data.dart';
import 'package:my_work/apiService/phone.dart';
class CarouselSliderData extends StatefulWidget{
const CarouselSliderData({super.key});
@override
State<CarouselSliderData> createState() => CarouselSliderDataState();
}
class CarouselSliderDataState extends State<CarouselSliderData> {
Phone? info;
@override
void initState() {
DioService.getDataMocky(
url:'https://run.mocky.io/v3/654bd15e-b121-49ba-a588-960956b15175'
).then((value) async {
if(value != null){
setState((){
info = value!;
});
}
}).catchError(
(value) => (value),
);
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image(image:)
],
);
}
}
第1步:从API获取json。我称这个变量为
映射<字符串,动态>json;
步骤2:如您所见,json的"home_store"
是一个电话列表。因此,您创建了一个名为PhoneModel
的模型类,如下所示:
class PhoneModel {
int id;
bool isNew;
String title;
String subtitle;
String picture;
bool isBuy;
/// constructor
PhoneModel({this.id ...});
/// from json to class model
PhoneModel.fromJson(Map<String, dynamic> json) {
this.id = json["id"];
this.isNew = json["is_new"];
...
}
}
然后像这样做以获取项目列表:
List<PhoneModel> listPhones = List<PhoneModel>.from(
(json['home_store'] as List).map((e) => PhoneModel.fromJson(e)),
);
现在,您的电话列表是可变的listPhones
。Url是字段picture
。做Image.network
或其他任何事情。。。祝你好运