【问题标题】:Flutter setState() doesn't reactive to the List data changeFlutter setState() 对列表数据更改没有反应
【发布时间】:2022-07-06 19:12:41
【问题描述】:

当我点击浮动按钮时,试图改变小部件,但没有成功,为什么?

List的元素数据确实发生了变化,但是widget并没有同步更新。

这个问题让我很困惑,不知道如何解决? 我希望 ListView 小部件能够响应列表数据的变化。

main.dart

import 'package:flutter/material.dart';

import 'video_model.dart';
import 'video_item.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var videos = [
    VideoModel(title: 'a', intro: 'x'),
    VideoModel(title: 'b', intro: 'y'),
    VideoModel(title: 'c', intro: 'z'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add),
        onPressed: () {
          setState(() {
            videos = [
              VideoModel(title: 'c', intro: 'o'),
              VideoModel(title: 'd', intro: 'p'),
              VideoModel(title: 'f', intro: 'q'),
            ];
          });
        },
      ),
      body: ListView.builder(
        shrinkWrap: true,
        physics: const ScrollPhysics(),
        itemCount: videos.length,
        itemBuilder: (context, index) {
          var video = videos[index];
          return VideoItem(video);
        },
      ),
    );
  }
}

video_item.dart

import 'package:flutter/material.dart';

import 'video_model.dart';

class VideoItem extends StatefulWidget {
  final VideoModel video;
  const VideoItem(this.video, {Key? key}) : super(key: key);

  @override
  State<VideoItem> createState() => _VideoItemState();
}

class _VideoItemState extends State<VideoItem> {
  late VideoModel video;

  @override
  void initState() {
    super.initState();
    video = widget.video;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Row(
        children: [
          Text(video.title),
          const SizedBox(width: 16),
          Text(video.intro)
        ],
      ),
    );
  }
}

video_model.dart

class VideoModel {
  String title;
  String intro;

  VideoModel({
    required this.title,
    required this.intro,
  });
}

【问题讨论】:

    标签: flutter list setstate


    【解决方案1】:

    video_item.dart 中,您在initState 中设置视频变量,该变量仅运行一次,因此每当您使用setState 时,只会再次调用build 方法。因此_VideoItemState 中的video 变量永远不会再次设置,因此它保持相同的旧值

    您可以像这样简单地使用 VideoItem 构造函数中的视频

    children: [
          Text(widget.video.title),
          const SizedBox(width: 16),
          Text(widget.video.intro)
        ],
    

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2019-06-07
      • 1970-01-01
      • 2020-05-27
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多