我有一个按钮启用 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
启用 GPS 的时间(否则它会吐出Observable.empty()
),我不太清楚如何在其中工作(可能是合并?我认为启用 GPS 时我应该能够设置控制器中的文本。