我的flutter/dart代码给了我一个红色的NosuchmethodError:.该方法'toDouble&



我正在尝试链接注册页面,在那里它会给我一个带有NosuchmethodError的红屏错误:。。方法"toDouble"。在过去的两天里,我一直被困在这个问题上,尝试了很多事情。我还在学习,所以还不是专家。我确实查阅了官方文件,但无济于事。它在模拟器中不起作用。视频供参考https://streamable.com/0qvcm5

import 'package:flutter/material.dart';
import 'package:morus/Screen/Login/login_screen.dart';
//import 'package:morus/Screen/Signup/signup_screen.dart';
import 'package:morus/screen/complete_profile/sign_up/sign_up_screen.dart';
//import 'package:morus/screen/complete_profile/complete_profile_screen.dart';
import 'package:morus/screen/complete_profile/sign_up/sign_up_screen.dart';
import 'package:morus/Screen/Welcome/components/background.dart';
import 'package:morus/screen/components/rounded_button.dart';
import 'package:morus/constants.dart';
import 'package:flutter_svg/svg.dart';
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
// This size provide us total height and width of our screen
return Background(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"WELCOME TO Moe",
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: size.height * 0.05),
SvgPicture.asset(
"assets/icons/chat.svg",
height: size.height * 0.45,
),
SizedBox(height: size.height * 0.05),
RoundedButton(
text: "LOGIN",
press: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return LoginScreen();
},
),
);
},
),
RoundedButton(
text: "SIGN UP",
color: kPrimaryLightColor,
textColor: Colors.black,
press: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return SignUpScreen();
},
),
);
}),
],
),
),
);
}
}

这是我的签名_up_screen.art

import 'package:flutter/material.dart';
import 'components/body.dart';
class SignUpScreen extends StatelessWidget {
static String routeName = "/sign_up";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sign Up"),
),
body: Body(),
);
}
}

这是我的签名_up_form.dart

import 'package:flutter/material.dart';
import 'package:morus/screen/components/custom_surfix_icon.dart';
import 'package:morus/screen/components/default_button.dart';
import 'package:morus/screen/components/form_error.dart';
import 'package:morus/screen/complete_profile/complete_profile_screen.dart';
//import 'package:shop_app/screens/login_success/login_success_screen.dart';
import '../../../../constants.dart';
import '../../../../size_config.dart';
class SignUpForm extends StatefulWidget {
@override
_SignUpFormState createState() => _SignUpFormState();
}
class _SignUpFormState extends State<SignUpForm> {
final _formKey = GlobalKey<FormState>();
String email;
String password;
String conform_password;
bool remember = false;
final List<String> errors = [];
void addError({String error}) {
if (!errors.contains(error))
setState(() {
errors.add(error);
});
}
void removeError({String error}) {
if (errors.contains(error))
setState(() {
errors.remove(error);
});
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
buildEmailFormField(),
SizedBox(height: getProportionateScreenHeight(30)),
buildPasswordFormField(),
SizedBox(height: getProportionateScreenHeight(30)),
buildConformPassFormField(),
FormError(errors: errors),
SizedBox(height: getProportionateScreenHeight(40)),
DefaultButton(
text: "Continue",
press: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
// if all are valid then go to success screen
Navigator.pushNamed(context, CompleteProfileScreen.routeName);
}
},
),
],
),
);
}
TextFormField buildConformPassFormField() {
return TextFormField(
obscureText: true,
onSaved: (newValue) => conform_password = newValue,
onChanged: (value) {
if (value.isNotEmpty) {
removeError(error: kPassNullError);
} else if (value.isNotEmpty && password == conform_password) {
removeError(error: kMatchPassError);
}
conform_password = value;
},
validator: (value) {
if (value.isEmpty) {
addError(error: kPassNullError);
return "";
} else if ((password != value)) {
addError(error: kMatchPassError);
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Confirm Password",
hintText: "Re-enter your password",
// If  you are using latest version of flutter then lable text and hint text shown like this
// if you r using flutter less then 1.20.* then maybe this is not working properly
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/Lock.svg"),
),
);
}
TextFormField buildPasswordFormField() {
return TextFormField(
obscureText: true,
onSaved: (newValue) => password = newValue,
onChanged: (value) {
if (value.isNotEmpty) {
removeError(error: kPassNullError);
} else if (value.length >= 8) {
removeError(error: kShortPassError);
}
password = value;
},
validator: (value) {
if (value.isEmpty) {
addError(error: kPassNullError);
return "";
} else if (value.length < 8) {
addError(error: kShortPassError);
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Password",
hintText: "Enter your password",
// If  you are using latest version of flutter then lable text and hint text shown like this
// if you r using flutter less then 1.20.* then maybe this is not working properly
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/Lock.svg"),
),
);
}
TextFormField buildEmailFormField() {
return TextFormField(
keyboardType: TextInputType.emailAddress,
onSaved: (newValue) => email = newValue,
onChanged: (value) {
if (value.isNotEmpty) {
removeError(error: kEmailNullError);
} else if (emailValidatorRegExp.hasMatch(value)) {
removeError(error: kInvalidEmailError);
}
return null;
},
validator: (value) {
if (value.isEmpty) {
addError(error: kEmailNullError);
return "";
} else if (!emailValidatorRegExp.hasMatch(value)) {
addError(error: kInvalidEmailError);
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Email",
hintText: "Enter your email",
// If  you are using latest version of flutter then lable text and hint text shown like this
// if you r using flutter less then 1.20.* then maybe this is not working properly
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/Mail.svg"),
),
);
}
}

这是车身.dart

import 'package:flutter/material.dart';
import 'package:morus/screen/components/socal_card.dart';
import 'package:morus/constants.dart';
import 'package:morus/size_config.dart';
import 'sign_up_form.dart';
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: SizedBox(
width: double.infinity,
child: Padding(
padding:
EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: SizeConfig.screenHeight * 0.04), // 4%
Text("Register Account", style: headingStyle),
Text(
"Complete your details or continue nwith social media",
textAlign: TextAlign.center,
),
SizedBox(height: SizeConfig.screenHeight * 0.08),
SignUpForm(),
SizedBox(height: SizeConfig.screenHeight * 0.08),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SocalCard(
icon: "assets/icons/google-icon.svg",
press: () {},
),
SocalCard(
icon: "assets/icons/facebook-2.svg",
press: () {},
),
SocalCard(
icon: "assets/icons/twitter.svg",
press: () {},
),
],
),
SizedBox(height: getProportionateScreenHeight(20)),
Text(
'By continuing your confirm that you agree nwith our Term and Condition',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption,
)
],
),
),
),
),
);
}
}

这是我的size_config.dart(它只提到了"double"(

import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData _mediaQueryData;
static double screenWidth;
static double screenHeight;
static double defaultSize;
static Orientation orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData.size.width;
screenHeight = _mediaQueryData.size.height;
orientation = _mediaQueryData.orientation;
}
}
// Get the proportionate height as per screen size
double getProportionateScreenHeight(double inputHeight) {
double screenHeight = SizeConfig.screenHeight;
// 812 is the layout height that designer use
return (inputHeight / 812.0) * screenHeight;
}
// Get the proportionate height as per screen size
double getProportionateScreenWidth(double inputWidth) {
double screenWidth = SizeConfig.screenWidth;
// 375 is the layout width that designer use
return (inputWidth / 375.0) * screenWidth;
}

非常感谢您的帮助。

@Doctor基于您的代码,我看到SizeConfig类在使用之前必须初始化。在页面中使用SizeConfig.init(context);之前,请尝试调用它

最新更新