流行颤动后刷新流

refresh stream after pop flutter

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

我的插件

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

}

}

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

我面临的问题是,当提供输入时,如果在下一个输入中提供空白提交,流将保留输入的先前值。数据库从流中获取先前的输入,而不是 'Not Provided',空白应该是这种情况。

我是 streams 的新手,据我所知,stream 的值在弹出并移动到 thankyou 屏幕后应该为 null。我错过了什么吗?我为此挠头了一段时间,我知道我缺少一些基本的东西,我将不胜感激 help.Thanks.

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

尝试跟随导航器。

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