【问题标题】:can not load data again with Stream flutter无法使用 Stream Flutter 再次加载数据
【发布时间】:2021-09-08 15:58:43
【问题描述】:

当应用程序启动时,我正在使用流数据从 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,
                                      ))
                              );}

应用启动时

当我转到其他屏幕并卷土重来时

【问题讨论】:

    标签: flutter dart stream-builder flutter-streambuilder


    【解决方案1】:

    试试这个

    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);
    }
    

    【讨论】:

    • @YetDev 嘿,请附上这个方法 weatherRepo.loadMainLocations();
    猜你喜欢
    • 2020-02-23
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2016-03-28
    • 2018-11-07
    • 2015-11-06
    • 2016-04-04
    相关资源
    最近更新 更多