我使用BLoC模式开发了一个应用程序。
在我的应用中,有2条路线,路线A和B,并且它们都访问相同的数据。
如下移动路线时引起的问题。

  • 从显示数据的路线A移至路线B。
  • 更新路线B上的数据。
  • 返回至路线A.

  • 回到路线A之后,显示数据的StreamBuilder永远不会自动更新。
    如何让StreamBuilder在恢复状态下更新?
    这是示例代码。
    路线
    class RouteA extends StatefulWidget {
      @override
      _RouteAState createState() => _RouteAState();
    }
    
    class _RouteAState extends State<RouteA> {
      @override
      Widget build(BuildContext context) {
        final bloc = Bloc();
        return Column(
          children: [
            StreamBuilder(    // this StreamBuilder never updates on resumed state
                stream: bloc.data, // mistake, fixed. before: bloc.count
                builder: (_, snapshot) => Text(
                      snapshot.data ?? "",
                    )),
            RaisedButton(
              child: Text("Move to route B"),
              onPressed: () {
                Navigator.of(context).pushNamed("routeB");
              },
            ),
          ],
        );
      }
    }
    
    routeB.dart
    class RouteB extends StatefulWidget {
      @override
      _RouteBState createState() => _RouteBState();
    }
    
    class _RouteBState extends State<RouteB> {
      @override
      Widget build(BuildContext context) {
        final bloc = Bloc();
        return Center(
          child: RaisedButton(
            child: Text("Update data"),
            onPressed: () {
              bloc.update.add(null);
            },
          ),
        );
      }
    }
    
    bloc.dart
    class Bloc {
      Stream<String> data;
      Sink<void> update;
      Model _model;
    
      Bloc() {
        _model = Model();
    
        final update = PublishSubject<void>();
        this.update = update;
    
        final data = BehaviorSubject<String>(seedValue: "");
        this.data = data;
    
        update.map((event) => _model.update()).listen((event) => data.sink.add(_model.getData()));
      }
    }
    
    模型
    class Model {
      static Model _model;
    
      factory Model() {    // model is singleton.
        _model ??= Model._();
        return _model;
      }
    
      Model._();
    
      int _data = 0;
    
      void update() {
        _data++;
      }
    
      String getData() {
        return _data.toString();
      }
    }
    

    最佳答案

    StreamBuilder会在数据更改时更新数据,而不仅仅是调用stream 路线A

    class RouteA extends StatefulWidget {
      @override
      _RouteAState createState() => _RouteAState();
    }
    
    class _RouteAState extends State<RouteA> {
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            StreamBuilder(    // this StreamBuilder never updates on resumed state
                stream: bloc.data, // mistake, fixed. before: bloc.count
                builder: (_, snapshot) => Text(
                  snapshot.data ?? "",
                )),
            RaisedButton(
              child: Text("Move to route B"),
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
                  return RouteB();
                }));
              },
            ),
          ],
        );
      }
    }
    
    B路线
    class RouteB extends StatefulWidget {
      @override
      _RouteBState createState() => _RouteBState();
    }
    
    class _RouteBState extends State<RouteB> {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: RaisedButton(
            child: Text("Update data"),
            onPressed: () {
              bloc.updateData();
            },
          ),
        );
      }
    }
    
    集团
    class Bloc {
      final _update = PublishSubject<String>();
      Model _model = Model();
      Stream<String> get data => _update.stream;
    
      void updateData() async {
        _model.update();
        _update.sink.add(_model.getData());
    
        _update.stream.listen((event) {
          print(event);
        });
      }
    
      dispose() {
        _update.close();
      }
    }
    
    final bloc = Bloc();
    
    只需遵循以上更改,它将为您解决问题。

    关于flutter - 如何使用Flutter在恢复状态下重建StreamBuilder,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63546686/

    10-14 21:25