2

当应用程序启动时,我正在使用流数据从 API 加载数据。但是它有一个问题,当它加载 SearchPage 小部件时,快照有数据,但是当我切换到其他屏幕并返回 SearchPage 小部件时,它丢失了所有数据.

class _SearchPageState extends State<SearchPage> with TickerProviderStateMixin {
  final locationsStream = StreamController<List<Weather>>.broadcast();
  late final AnimationController _controller = AnimationController(
    duration: const Duration(seconds: 2),
    vsync: this,
  )..repeat(reverse: true);
  late final Animation<double> _animation = CurvedAnimation(
    parent: _controller,
    curve: Curves.elasticOut,
  );

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
     locationsStream.close();
  }

  @override
  void initState() {
    loadData();
    // TODO: implement initState
    super.initState();
  }

  void loadData() async {
    WeatherRepo weatherRepo = WeatherRepo();
    List<Weather> rss = await weatherRepo.loadMainLocations();
    locationsStream.sink.add(rss);
  }

我调用流数据时的代码:

 Container(
                        height: 300,
                        child: StreamBuilder<List<Weather>>(
                            stream: locationsStream.stream,
                            initialData: null,
                            builder: (context, snap) {
                              if (!snap.hasData) {
                                return Container(
                                  child: Center(
                                    child: CircularProgressIndicator(),
                                  ),
                                );
                              }
                              final data = snap.data;
                              return Container(
                                  height: 300,
                                  child: CarouselSlider(
                                      items: <Widget>[
                                        for (Weather w in data!) MainLocation(location: w),
                                      ],
                                      options: CarouselOptions(
                                        height: 300.0,
                                        autoPlay: true,
                                        autoPlayInterval: Duration(seconds: 3),
                                        autoPlayAnimationDuration: Duration(milliseconds: 2000),
                                        autoPlayCurve: Curves.fastOutSlowIn,
                                        enlargeCenterPage: true,
                                        scrollDirection: Axis.horizontal,
                                      ))
                              );}

应用程序启动时 在此处输入图像描述

当我去其他屏幕并卷土重来时 在此处输入图像描述

4

1 回答 1

0

尝试这个

final StreamController<List<Weather>> locationsStream =
      StreamController<List<Weather>>.broadcast();

@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) async {
   await loadData();
  });
}

void loadData() async {
 WeatherRepo weatherRepo = WeatherRepo();
 List<Weather> rss = await weatherRepo.loadMainLocations();
 locationsStream.add(rss);
}
于 2021-06-25T08:47:00.770 回答