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

标签 flutter dart flutter-layout

我是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/

相关文章:

firebase - 如何获得 flutter 朔迷离中的Firestore文档的证件编号?

flutter - 如何使用 flutter bloc 通过使用 firestore 的依赖注入(inject)来处理错误 `The getter was called on null`

flutter - 如何让 float 标签停留在边框内

constructor - Dart中重定向到另一个构造函数时,是否需要提供所有参数?

flutter - 如何在GridView中每6个不同的项目渲染一个图像?

flutter - ChangeNotifierProxyProvider 构建器与更新问题

Flutter DataTable 选择行

dart - 为什么Dart编辑器仍在运行

flutter - 如何在 flutter 中使水平菜单自动滚动并垂直产品列表

flutter - 如何防止Flutter剪切BoxDecoration阴影?