后期初始化错误显示,在第一次加载screen.datas时,只有在我热重新加载我的app.please之后,才能帮助我解决这个问题。获取单个参与者的详细信息是一个api。我要用Getx Obx完成它。
提前谢谢。
这是我需要读取到我的应用程序的API的数据。
{
"adult": false,
"also_known_as": [
" Edward Harrison ",
"Ed Norton",
"爱德华·诺顿",
"เอ็ดเวิร์ด นอร์ตัน",
"Эдвард Нортон",
"エドワード・ノートン",
"إدوارد نورتون",
"에드워드 노튼",
"Έντουαρντ Νόρτον",
"Edward Harrison Norton",
"אדוארד נורטון",
"ادوارد نورتون"
],
"biography": "Edward Harrison Norton (born August 18, 1969) is an American actor and filmmaker. He has received numerous awards and nominations, including a Golden Globe Award and three Academy Award nominations.\n\nBorn in Boston, Massachusetts and raised in Columbia, Maryland, Norton was drawn to theatrical productions at local venues as a child. After graduating from Yale College in 1991, he worked for a few months in Japan before moving to New York City to pursue an acting career. He gained immediate recognition and critical acclaim for his debut in Primal Fear (1996), which earned him a Golden Globe for Best Supporting Actor and an Academy Award nomination in the same category. His role as a reformed neo-Nazi in American History X (1998) earned him an Academy Award nomination for Best Actor. He also starred in the film Fight Club (1999), which garnered a cult following.\n\nNorton emerged as a filmmaker in the 2000s. He established the production company Class 5 Films in 2003, and was director or producer of the films Keeping the Faith (2000), Down in the Valley (2005), and The Painted Veil (2006). He continued to receive critical acclaim for his acting roles in films such as The Score (2001), 25th Hour (2002), The Illusionist (2006), Moonrise Kingdom (2012), and The Grand Budapest Hotel (2014). His biggest commercial successes have been Red Dragon (2002), Kingdom of Heaven (2005), The Incredible Hulk (2008), and The Bourne Legacy (2012). For his role in the black comedy Birdman (2014), Norton earned another Academy Award nomination for Best Supporting Actor.",
"birthday": "1969-08-18",
"deathday": null,
"gender": 2,
"homepage": null,
"id": 819,
"imdb_id": "nm0001570",
"known_for_department": "Acting",
"name": "Edward Norton",
"place_of_birth": "Boston, Massachusetts, USA",
"popularity": 11.598,
"profile_path": "/5XBzD5WuTyVQZeS4VI25z2moMeY.jpg"
}这是我从quicktype生成的模型,
import 'dart:convert';
ActorModel actorModelFromJson(String str) => ActorModel.fromJson(json.decode(str));
String actorModelToJson(ActorModel data) => json.encode(data.toJson());
class ActorModel {
ActorModel({
this.adult,
this.alsoKnownAs,
this.biography,
this.birthday,
this.deathday,
this.gender,
this.homepage,
this.id,
this.imdbId,
this.knownForDepartment,
this.name,
this.placeOfBirth,
this.popularity,
this.profilePath,
});
bool? adult;
List<String>? alsoKnownAs;
String? biography;
DateTime? birthday;
dynamic deathday;
int? gender;
dynamic? homepage;
int? id;
String? imdbId;
String? knownForDepartment;
String? name;
String? placeOfBirth;
double? popularity;
String? profilePath;
factory ActorModel.fromJson(Map<String, dynamic> json) => ActorModel(
adult: json["adult"],
alsoKnownAs: List<String>.from(json["also_known_as"].map((x) => x)),
biography: json["biography"],
birthday: DateTime.parse(json["birthday"]),
deathday: json["deathday"],
gender: json["gender"],
homepage: json["homepage"],
id: json["id"],
imdbId: json["imdb_id"],
knownForDepartment: json["known_for_department"],
name: json["name"],
placeOfBirth: json["place_of_birth"],
popularity: json["popularity"].toDouble(),
profilePath: json["profile_path"],
);
Map<String, dynamic> toJson() => {
"adult": adult,
"also_known_as": List<dynamic>.from(alsoKnownAs!.map((x) => x)),
"biography": biography,
"birthday": "${birthday!.year.toString().padLeft(4, '0')}-${birthday!.month.toString().padLeft(2, '0')}-${birthday!.day.toString().padLeft(2, '0')}",
"deathday": deathday,
"gender": gender,
"homepage": homepage,
"id": id,
"imdb_id": imdbId,
"known_for_department": knownForDepartment,
"name": name,
"place_of_birth": placeOfBirth,
"popularity": popularity,
"profile_path": profilePath,
};
}下面是我的Api获取页面代码:
class ActorService{
late ActorModel actors;
Future<ActorModel> getActorDetails()async{
final url = 'https://api.themoviedb.org/3/person/819?api_key=++++api key++++&language=en-US';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
actors = ActorModel.fromJson(data);
}
return actors;
}
}这是我的Getx控制器页面:
class ActorController extends GetxController{
late ActorModel actors;
@override
void onInit() {
getActorInfo();
super.onInit();
}
void getActorInfo()async{
var actorInfo = await ActorService().getActorDetails();
if (actorInfo != null) {
actors = actorInfo;
}
}
}这就是我在屏幕上读取数据的方式。
Text(actorController.actors.name.toString()),发布于 2022-12-01 17:14:00
首先,您必须在RxVariable类中创建GetXController,以便使用模型类存储数据。
1.Rx<ActorModel> actorModelDetail = ActorModel().obs;然后调用api并将数据存储到RxVariable中。
2.actorModelDetail.value = ActorModel(...);之后,在您的无状态或有状态小部件类中使用这个变量,然后用Obx包装这个varible。
///Declare this in your stateless or stateful widget.
3.ActorController actorController = Get.find();
Obx(() => Text(actorController.actorModelDetail.value.name))https://stackoverflow.com/questions/74645269
复制相似问题