我有个按钮可以启动GPS。当GPS启用时,TextField应该填充纬度值。禁用时,用户应该能够输入纬度值。我需要验证用户提供的值(范围为-90.0到90.0)。
类似于:
StreamBuilder(
stream: bloc.latitude,
builder: (context, snapshot) {
return TextField(
keyboardType: TextInputType.number,
decoration: new InputDecoration(
hintText: 'Latitude',
labelText: 'Latitude',
errorText: snapshot.error),
onChanged: bloc.onLatitudeChanged,
//enabled: snapshot.hasData,
controller: _latitudeTextController,
);
},
),我的集团有:
final _latitudeController = BehaviorSubject<String>();
Function(String) get onLatitudeChanged => _latitudeController.sink.add;
Stream<String> get latitude => _latitudeController
.distinct()
.map((l) => double.parse(l))
.transform(validateLatitude) // double -> double
.map((l) => l.toString());我有很多问题。如果map失败了,我就会得到一个FormatException,我不知道如何处理这个问题。当全球定位系统启用时,我的集团也有一个Stream<double> currentLatitude (否则它会吐出Observable.empty()),我不知道如何在其中工作(可能是合并?)我认为当GPS启用时,我应该能够设置控制器中的文本。
发布于 2018-12-13 20:38:44
我认为当使用Stream时,您只需要使用snapshot.hasData()或snapshot.hasError()来检查快照中的数据。这至少可以解决Observable.empty()问题。对于FormatException,需要在验证函数中验证这一点。希望这能有所帮助!
可能是这样的
final TextEditingController _controller = new TextEditingController();
if (snapshot.hasData) {
_controller.text = snapshot.data;
} 注意:将此_controller分配给TextField中的控制器。
https://stackoverflow.com/questions/53766128
复制相似问题