【问题标题】:refresh stream after pop flutter弹出颤动后刷新流
【发布时间】:2020-05-10 17:25:32
【问题描述】:

我正在尝试构建一个带有文本字段的屏幕,该屏幕接受输入并使用流保存在数据库中。

我的小部件

class _PhoneInputViewState extends State<PhoneInputView> {

   RatingsBloc _rbloc;

  @override
  void didChangeDependencies() {
    _rbloc =  Provider.of(context).fetch(RatingsBloc);
    super.didChangeDependencies();
  }

  @override
  void dispose() {

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    String f = _rbloc.getCustomerPhone;
    return Container(
      child: Column(
        children: <Widget>[
          StreamBuilder(
            stream: _rbloc.ratingCustomer$,
            builder: (context, snapshot) {
              return TextField(
                onChanged: _rbloc.changeRatingCustomer,
                  decoration: InputDecoration (
                    // to test
                  hintText: '$f',
                  errorText: snapshot.error,
                  border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(30)
              ),
              ));
            }
          ),
          StreamBuilder(
            stream: _rbloc.ratingCustomerPhone$,
            builder: (context, snapshot) {
              return TextField(
                 onChanged: _rbloc.changeRatingCustomerPhone,
                  decoration: InputDecoration (
                    // to test
                    hintText: '$f',
                    errorText: snapshot.error,
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30)
                    ),
                  ),
              );
            }
          ),
          RaisedButton(
            child: Text('submit'),
            onPressed: (){
              _rbloc.submit();
              Navigator.pop(context);
              Navigator.push(context, MaterialPageRoute(builder: (context) =>
                  ThankYouScreen()));
            },
          )
        ],
      ),

    );
  }
}

我的圈子

class RatingsBloc with Validators  implements Bloc{

  final _repository = Repository();
  final _ratingCustomer = BehaviorSubject<String>();
  final _ratingCustomerPhone = BehaviorSubject<String>();


//add data
  Observable<String>  get ratingCustomer$ => _ratingCustomer.stream.transform(nameValidator);
  Observable<String>  get ratingCustomerPhone$ => _ratingCustomerPhone.stream.transform(phoneValidator);


//get data

  String get getCustomerPhone => _ratingCustomerPhone.value;

// changed data

  Function(String) get changeRatingCustomer => _ratingCustomer.sink.add;
  Function(String) get changeRatingCustomerPhone => _ratingCustomerPhone.sink.add;

//Futures

  Future <Map<String,dynamic>> addRatings() async {
    if(_ratingCustomerPhone.value ==null){_ratingCustomerPhone.sink.add(notProvided);}
    if(_ratingCustomer.value ==null){_ratingCustomer.sink.add(notProvided);}
    Map<String,dynamic> r = {
      'customerName':_ratingCustomer.value,
      'customerPhone' :_ratingCustomerPhone.value,
    };
     _repository.addRatings(r);
    return r;
  }


submit() async{
    await addRatings();

  }

//dispose
dispose(){
  _ratingCustomerPhone.close();
  _ratingCustomer.close();

}

}

这里我想在 db 中添加 customerName 和 customerPhone 的值 --> 提交后移动到感谢屏幕 --> 然后在延迟一段时间后返回到 phoneInput 小部件并再次输入。输入是可选的,用户也可以在没有任何值的情况下按下提交。

我面临的问题是提供输入时,如果在下一个输入中提供空白提交,则流将保留输入的先前值。 db 从流中获取先前的输入,而不是“未提供”,这应该是空白的情况。

我是流的新手,据我了解,流的值在弹出并移动到感谢屏幕后应该为空。我是不是错过了什么。一段时间以来,我为此挠头,我知道我缺少一些基本的东西,我将不胜感激。谢谢。

【问题讨论】:

    标签: flutter dart flutter-navigation


    【解决方案1】:

    问题是,当您从流中返回时,它已经包含上次输入的值,因此您必须对其进行清理。

    尝试跟随导航器。

    Navigator.push(context,
                      MaterialPageRoute(builder: (context) => ThankYouScreen())).then((value){
                        _rbloc.changeRatingCustomer('');
                        _rbloc.changeRatingCustomerPhone('');
                      });
    

    【讨论】:

    • 感谢 Viren,这行得通。但我猜即使小部件关闭,流也会保留该值,以使其可用于全局状态。我仍然在溪流周围徘徊。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2020-05-12
    • 1970-01-01
    • 2015-09-26
    • 2021-04-18
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多