我有一个TextField,其中允许输入1,4之类的数字。我正在尝试将替换为。因此数字将为1.4。
我正在尝试
double alphareceipe = double.parse(_alphareceipe.text);
alphareceipe = alphareceipe.replace("," ".");
但是出现此错误:
错误:未为类double定义方法replace。 ([brewingapp] lib / screens / calculator / alphaacid.dart:227中的undefined_method)。总代码如下:
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';
import 'package:brewingapp/app_localizations.dart';
import 'package:flutter/services.dart';
import 'package:devicelocale/devicelocale.dart';
const String testDevice = "Mobile_id";
class AlphaAcid extends StatefulWidget {
@override
_AlphaAcidState createState() => _AlphaAcidState();
}
class _AlphaAcidState extends State<AlphaAcid> {
}
String _emiResult = "";
final TextEditingController _weight = TextEditingController();
final TextEditingController _alphareceipe = TextEditingController();
final TextEditingController _alphanew = TextEditingController();
@override
void initState() {
initPlatformState();
super.initState();
}
@override
String _locale;
Future<void> initPlatformState() async {
String currentLocale;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
currentLocale = await Devicelocale.currentLocale;
print(currentLocale);
} on PlatformException {
print("Error obtaining current locale");
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_locale = currentLocale;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context).translate('Hopsalpha'),),
backgroundColor: Colors.green[800],
//elevation: 0.0,
),
body: Center(
child: Container(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.green[800],
controller: _alphareceipe,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green[800], width: 2.0,),
),
filled: true,
fillColor: Colors.green[10],
labelText: _locale != 'da-DK'
? "Alpha acid receipe"
: "Alpha syre opskrift",
labelStyle: TextStyle(color: Colors.green[800])
),
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
// WhitelistingTextInputFormatter.digitsOnly
],
),
),
Container(
padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.green[800],
controller: _alphanew,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green[800], width: 2.0,),
),
filled: true,
fillColor: Colors.green[10],
//labelText: "Final Gravity (1018)",
labelText: _locale != 'da-DK'
? "Your alpha acid"
: "Din alpha syre",
labelStyle: TextStyle(color: Colors.green[800]),
),
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
//WhitelistingTextInputFormatter.digitsOnly
],
),
),
Container(
padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.green[800],
controller: _weight,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green[800], width: 2.0,),
),
filled: true,
fillColor: Colors.green[10],
//labelText: "Final Gravity (1018)",
labelText: _locale != 'da-DK'
? "Weight receipe"
: "Vægt i opskrift",
labelStyle: TextStyle(color: Colors.green[800]),
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly
],
),
),
Flexible(
fit: FlexFit.loose,
child: FlatButton(
onPressed: _handleCalculation,
//child: Text("Calculate"),
child: Text(AppLocalizations.of(context).translate('calculate'),),
color: Colors.green[600],
textColor: Colors.white,
padding: EdgeInsets.only(top: 10.0, bottom: 10.0, left: 24.0, right: 24.0),
),
),
emiResultsWidget(_emiResult)
],
),
),
));
}
void _handleCalculation() {
double newWeight = 0.0;
//
//
//
//
//
double alphareceipe = double.parse(_alphareceipe.text);
alphareceipe = alphareceipe.replace("," ".");
int alphanew =int.parse(_alphanew.text);
int weightreceipe = int.parse(_weight.text);
//
try {
newWeight = (weightreceipe * alphareceipe) / alphanew;
_emiResult = newWeight.toStringAsFixed(0);
setState(() {
});
} //catch(e) {
on Exception catch(e){
print(e);
setState(() {
});
}
//_emiResult = A.toStringAsFixed(1);
//setState(() {
//});
}
Widget emiResultsWidget(emiResult) {
bool canShow = false;
String _emiResult = emiResult;
if(_emiResult.length > 0) {
canShow = true;
}
return
Container(
margin: EdgeInsets.only(top: 30.0, left: 20.0, right: 20.0),
alignment: FractionalOffset.center,
color: Colors.green[50],
child: canShow ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
//crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget> [
Padding(
padding: EdgeInsets.all(10.0),
child:
Text(AppLocalizations.of(context).translate('newweight'),
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.green[800],
),
)
),
SizedBox(width: 20.0,),
Text(_emiResult,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.green[800],
)
),
SizedBox(width: 10.0,),
]),
]
) : Container(),
);
}
}
答案 0 :(得分:1)
不能在Double上使用String函数。替换字符串上的符号,然后将其强制转换为两倍。
答案 1 :(得分:1)
在文本已转换为double
后,您尝试替换字符。尝试以下方法:
double alphareceipe = double.parse(_alphareceipe.text.replaceAll(",", "."));