Futtler 错误在 null 上调用了方法“toDouble”。接收方:null 尝试调用:toDouble() [重复]

Posted

技术标签:

【中文标题】Futtler 错误在 null 上调用了方法“toDouble”。接收方:null 尝试调用:toDouble() [重复]【英文标题】:Futtler error The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble() [duplicate] 【发布时间】:2021-02-04 08:50:45 【问题描述】:

方法“toDouble”在 null 上被调用。接收方:null 尝试调用:toDouble()....................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ................................................

Exception caught by widgets library 

The following NoSuchMethodError was thrown building Body(dirty):
The method 'toDouble' was called on null.
Receiver: null
Tried calling: toDouble()

The relevant error-causing widget was
Body
lib\…\Sign_in\SignInPage.dart:14
When the exception was thrown, this was the stack
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      double.* (dart:core-patch/double.dart:36:23)
#2      getProportionateScreenWidth
package:BFpropertyMb/settings/size_config.dart:29
#3      Body.build
package:BFpropertyMb/…/components/body.dart:16
#4      StatelessElement.build

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;

import 'package:BFpropertyMb/components/custom_surfix_icon.dart';
import 'package:BFpropertyMb/components/default_button.dart';
import 'package:BFpropertyMb/components/form_error.dart';
import 'package:BFpropertyMb/settings/constants.dart';
import 'package:BFpropertyMb/settings/size_config.dart';
import 'package:flutter/material.dart';

class SignForm extends StatefulWidget 
  @override
  _SignFormState createState() => _SignFormState();


class _SignFormState extends State<SignForm> 
  final _formKey = GlobalKey<FormState>();
  String email;
  String 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)),
          Row(
            children: [
              Checkbox(
                value: remember,
                activeColor: kPrimaryColor,
                onChanged: (value) 
                  setState(() 
                    remember = value;
                  );
                ,
              ),
              Text("Remember me"),
              Spacer(),
              GestureDetector(
                onTap: () => Navigator.pushNamed(
                    context, null),
                child: Text(
                  "Forgot Password",
                  style: TextStyle(decoration: TextDecoration.underline),
                ),
              )
            ],
          ),
          FormError(errors: errors),
          SizedBox(height: getProportionateScreenHeight(20)),
          DefaultButton(
            text: "Continue",
            press: () 
              if (_formKey.currentState.validate()) 
                _formKey.currentState.save();
                // if all are valid then go to success screen
                Navigator.pushNamed(context,null);
              
            ,
          ),
        ],
      ),
    );
  

  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);
        
        return null;
      ,
      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"),
      ),
    );
  

【问题讨论】:

@paemapod,你是怎么解决的? 【参考方案1】:

您的getProportionateScreenWidth 函数似乎被inputWidth = null 调用。

请进一步检查您的代码。

【讨论】:

以上是关于Futtler 错误在 null 上调用了方法“toDouble”。接收方:null 尝试调用:toDouble() [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 错误在 null 上调用了方法“[]”。接收方:null 尝试调用:[]("videoId")

Firebase 身份验证,错误:NoSuchMethodError:在 null 上调用了方法“登录”

错误“在 null 上调用了方法 '[]'。接收方:null 尝试调用:[]("0tm2JqPY0oNq5vSM74BqOufhGao1")”

在 null 上调用了方法“[]”

Flutter NoSuchMethodError:在 null 上调用了方法“[]”。使用flutter获取api时如何解决此错误

Flutter:在 null 上调用了方法“[]”