【问题标题】:Flutter firestore shared preference double type data issueFlutter firestore 共享首选项双类型数据问题
【发布时间】:2020-12-07 23:29:35
【问题描述】:

在我的颤振应用程序中,当用户注册时,我需要保存双重类型的“身高”数据,我为此使用 SharedPreferences,但我遇到了问题。

这是我的SharedPreferences 代码:

static Future<bool> saveUserWeightSharedPreference(double userWeight) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setDouble(sharedPreferenceUserLoggedInKey, userWeight);
}

这是我的注册功能,我在哪里使用SharedPreferences。错误是:

参数类型“字符串”不能分配给参数类型 '双倍的'。 (argument_type_not_assignable 在 [spor3] lib\login\signup.dart:52)

  signMeUp(){

    if(formKey.currentState.validate()){

      Map<String, String> userInfoMap = {
        "name":userNameTextEditingController.text,
        "email": emailTextEditingController.text,
        "height": heightTextEditingController.text,
        "weight": weightTextEditingController.**text**, // this "text" codes not working
        "weightTwo": weightTwoTextEditingController.text,
      };

      HelperFunctions.saveUserEmailSharedPreference(emailTextEditingController.text);
      HelperFunctions.saveUserEmailSharedPreference(userNameTextEditingController.text,);
      HelperFunctions.saveUserHeightSharedPreference(heightTextEditingController.text,);
      HelperFunctions.saveUserWeightSharedPreference(weightTextEditingController.**text**,);// and this 
      "text" codes not working
      HelperFunctions.saveUserWeightTwoSharedPreference(weightTwoTextEditingController.text,);




      setState(() {
        isLoading = true;
      });
      authMethods.signUpWithEmailAndPassword(emailTextEditingController.text, passwordTextEditingController.text).then((val){



        databaseMethods.uploadUserInfoTwo(userInfoMap);
        HelperFunctions.saveUserLoggedInSharedPreference(true);
        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => MainPage()));
      });
    }
  }

编辑 1:我更改了我的 signMeUp() 函数,但这次我遇到了另一个问题

signMeUp(){


if(formKey.currentState.validate()){

  Map<String, dynamic> userInfoMap = {
    "name":userNameTextEditingController.text,
    "email": emailTextEditingController.text,
    "height": heightTextEditingController.text,
    "weight": double.parse(weightTextEditingController.text),
    "weightTwo": weightTwoTextEditingController.text,
  };

  HelperFunctions.saveUserEmailSharedPreference(emailTextEditingController.text);
  HelperFunctions.saveUserEmailSharedPreference(userNameTextEditingController.text,);
  HelperFunctions.saveUserHeightSharedPreference(heightTextEditingController.text,);
  HelperFunctions.saveUserWeightSharedPreference(double.parse(weightTextEditingController.text),);
  HelperFunctions.saveUserWeightTwoSharedPreference(weightTwoTextEditingController.text,);

  
  setState(() {
    isLoading = true;
  });
  authMethods.signUpWithEmailAndPassword(emailTextEditingController.text, passwordTextEditingController.text).then((val){

    
    databaseMethods.uploadUserInfoTwo(userInfoMap);
    HelperFunctions.saveUserLoggedInSharedPreference(true);
    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => MainPage()));
  });
}
}

问题:

在处理手势时引发以下 FormatException: 无效的双重

当异常被抛出时,这是堆栈: #0 double.parse (dart:core-patch/double_patch.dart:111:28)

【问题讨论】:

    标签: android firebase flutter google-cloud-firestore


    【解决方案1】:

    TextEditingController 总是为text 属性link 返回一个String。因此,您只需将其解析为双倍,然后将其传递给您的函数。

    HelperFunctions.saveUserWeightSharedPreference(double.parse(weightTextEditingController.text),);
    

    【讨论】:

    • 当我做这个颤动时给出了这个问题:处理手势时抛出了以下 FormatException:Invalid double
    • "weight": double.parse(weightTextEditingController.text), HelperFunctions.saveUserWeightSharedPreference(double.parse(weightTextEditingController.text),);我就这样变了
    • 如果用户输入的值不能被解析为double,你会得到一个异常。您需要验证输入或/并改用tryParsedouble.tryParse(blahblah) ?? 0.0api.flutter.dev/flutter/dart-core/double/tryParse.html
    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多