gpt4 book ai didi

flutter - 在Flutter中更新模态类属性时,UI不更新

转载 作者:行者123 更新时间:2023-12-03 03:32:26 25 4
gpt4 key购买 nike

我是Flutter的新手,在List中呈现UI时遇到问题。 UI不呈现新数据。
基本上,我的列表保存的数据类型为Movie

class Movie {
Movie(int id, String title, String thumbnail, String director,
[double rating, double price, double discountPrice]) {
this.id = id;
this.title = title;
this.thumbnail = thumbnail;
this.director = director;
this.rating = rating;
this.price = price;
this.discountPrice = discountPrice;
}

int id;
String thumbnail;
String title;
String director;
double rating = 0;
double price = 0;
double discountPrice = 9000;
}
在第一次开发时,我创建的类没有 discountPrice,然后在第二次迭代中添加了它。但是在热重装和重新启动中, discountPrice不会更新到UI。
这是我的小部件
class MovieInformation extends StatelessWidget {
final Movie movie;

MovieInformation({this.movie}) : super(key: ObjectKey(movie));

@override
Widget build(BuildContext context) {
return Container(
height: 140,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(
const Radius.circular(8),
),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Color(0x90d5e0e8),
offset: Offset(0, 10),
blurRadius: 16,
)
],
),
padding: const EdgeInsets.only(left: 110),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 12, left: 16),
child: Text(
'\$${movie.price}',
style: TextStyle(fontSize: 12),
),
),
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 12, left: 4),
child: Text(
'\$${movie.discountPrice}',
style: TextStyle(
fontSize: 12,
color: Colors.black45,
decoration: TextDecoration.lineThrough,
),
),
),
],
),
],
),
);
}
}
这是我的主类
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Color(0xFFF8F8F8),
),
home: Scaffold(
appBar: AppBar(
elevation: 0,
leading: IconButton(
icon: Icon(Icons.chevron_left),
onPressed: () => {},
),
),
body: HomePage(
movies: [
Movie(
1,
'The Astronaut',
"https://d1csarkz8obe9u.cloudfront.net/posterpreviews/space-movie-poster-design-template-18133e937d93002c68b4649ea234d75f_screen.jpg",
'John Doe',
4.5,
100, // discountPrice is omitted which expected to use default value : 9000
),
],
),
),
);
}
}
我使用此代码段建立列表
Expanded(
child: ListView.builder(
itemCount: movies.length,
itemBuilder: (context, index) {
return MovieItem(
movie: movies[index],
);
},
),
)
这是预览:
供个人使用和培训。 Source
App Preview

最佳答案

在“电影构造器”中,您已使用DiscountPrice作为可选的位置参数。如果没有值传递给可选的位置参数,则默认值为null。
因此,此处的this.discountPrice = discountPrice null被分配给discountPrice。 (成员变量)//这将覆盖您的9000值。
解决方案向可选的位置参数提供默认值,如下所示:

class Movie {
Movie(int id, String title, String thumbnail, String director,
[double rating, double price, double discountPrice = 9000]) {
this.id = id;
this.title = title;
this.thumbnail = thumbnail;
this.director = director;
this.rating = rating;
this.price = price;
this.discountPrice = discountPrice;
}

int id;
String thumbnail;
String title;
String director;
double rating = 0;
double price = 0;
double discountPrice;
}

关于flutter - 在Flutter中更新模态类属性时,UI不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63125229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com