我試圖從 datepicker 計算年齡,但在那個 datepicker 中我添加了 sharedpreference。為了計算年齡,我得到了 "dropdownValueBirthday" 等于 "birthday" 。然后顯示 null 。我如何解決這個錯誤?但是生日很好地顯示我想從生日計算年齡,并且應該將年齡顯示為持續時間。
圖片
持續時間仍然為空
代碼
void initState() {
super.initState();
dropdownValueBirthday = birthday.first;
checkValueBirthday();
}
//age
String age = "";
DateDuration? duration;
//date picker
//date picker
DateTime? selectedDate;
DateTime now = new DateTime.now();
void showDatePicker() {
DateTime mindate = DateTime(now.year - 2, now.month, now.day - 29);
DateTime maxdate = DateTime(now.year - 1, now.month, now.day);
showCupertinoModalPopup(
context: context,
builder: (BuildContext builder) {
return Container(
height: MediaQuery.of(context).copyWith().size.height * 0.25,
color: Colors.white,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: mindate,
onDateTimeChanged: (valueBirth) {
if (valueBirth != selectedDate) {
setState(() {
selectedDate = valueBirth;
dropdownValueBirthday =
'${selectedDate?.year}/${selectedDate?.month}/${selectedDate?.day} ';
calAge();
});
}
},
maximumDate: maxdate,
minimumDate: mindate,
),
);
});
}
void calAge() {
DateTime birthday = DateTime.parse(dropdownValueBirthday!);
setState(() {
duration = AgeCalculator.age(birthday);
});
// print('Your age is $duration');
}
String? dropdownValueBirthday;
List<String> birthday = [
'Birthday',
];
checkValueBirthday() {
_getDataBirthday();
}
_saveDataBirthday(String dropdownValueBirthdayShared) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString("dataBirthday", dropdownValueBirthdayShared);
}
_getDataBirthday() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
dropdownValueBirthday =
sharedPreferences.getString("dataBirthday") ?? birthday.first;
setState(() {});
}
小部件代碼
child: GestureDetector(
onTap: (showDatePicker),
child: SizedBox(
width: 110.0,
height: 25.0,
child: DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white,
),
child: Center(
child: Text(
selectedDate == null
? (dropdownValueBirthday ??
birthday.first)
: '${selectedDate?.year}/${selectedDate?.month}/${selectedDate?.day} ',
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500),
),
),
),
),
),
列印
新錯誤
uj5u.com熱心網友回復:
您需要將字串轉換為 DateTime
DateTime? birthday = DateTime.parse(dropdownValueBirthday);
uj5u.com熱心網友回復:
要將 String 轉換為 DateTime,您不能使用as
. as
如果物件的實際型別是它的子型別,則可以使用。您可以使用 來檢查型別is
。例如:
void main() {
ParentClass actualParent = ParentClass();
ParentClass hiddenChild = ChildClass();
ChildClass actualChild = ChildClass();
print(actualParent is ChildClass); // false
print(hiddenChild is ChildClass); // true
print(actualChild is ChildClass); // true
}
class ParentClass {
String getType() => 'parent';
}
class ChildClass extends ParentClass {
@override
String getType() => 'child';
}
如果要將 String 轉換為 DateTime,則必須使用DateTime.tryParse(String)
. 有關 DateTime 的更多資訊,請參閱官方檔案。
編輯
要按生日計算年齡,您需要從那天到今天(或任何其他日期)之間的持續時間。
void main() {
DateTime birthday = DateTime.parse("1970-01-01");
DateTime today = DateTime.now();
Duration age = today.difference(birthday);
print('${age.inDays} days'); // 19297 days (at 2022-11-01)
}
uj5u.com熱心網友回復:
試試下面的代碼:
void initState() {
super.initState();
dropdownValueBirthday = birthday.first;
checkValueBirthday();
}
//show date picker
//age
//Radio button variable declare
String age = "";
DateDuration? duration;
//date picker
//date picker
DateTime? selectedDate;
DateTime now = new DateTime.now();
void showDatePicker() {
DateTime mindate = DateTime(now.year - 2, now.month, now.day - 29);
DateTime maxdate = DateTime(now.year - 1, now.month, now.day);
showCupertinoModalPopup(
context: context,
builder: (BuildContext builder) {
return Container(
height: MediaQuery.of(context).copyWith().size.height * 0.25,
color: Colors.white,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: mindate,
onDateTimeChanged: (valueBirth) {
if (valueBirth != selectedDate) {
setState(() {
selectedDate = valueBirth;
dropdownValueBirthday =
'${selectedDate?.year}/${selectedDate?.month}/${selectedDate?.day} ';
});
}
},
maximumDate: maxdate,
minimumDate: mindate,
),
);
});
}
void calAge() {
DateTime birthday = DateTime.parse(dropdownValueBirthday);
setState(() {
duration = AgeCalculator.age(birthday);
});
// print('Your age is $duration');
}
String? dropdownValueBirthday;
List<String> birthday = [
'Birthday',
];
checkValueBirthday() {
_getDataBirthday();
}
_saveDataBirthday(String dropdownValueBirthdayShared) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString("dataBirthday", dropdownValueBirthdayShared);
}
_getDataBirthday() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
dropdownValueBirthday =
sharedPreferences.getString("dataBirthday") ?? birthday.first;
setState(() {});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524837.html
標籤:扑
下一篇:影響2種不同狀態的按鈕