简体   繁体   English

如何将字符串转换为 flutter 中的 integer 的时、分、秒时间?

[英]How to convert String to Time in Hour, Minute, Second as integer in flutter?

I am making an app in that I want to send a notification to the user with the help of the flutter_local_notification package.我正在制作一个应用程序,我想借助 flutter_local_notification package 向用户发送通知。 I want to send a notification to the user at a certain time every day, for that, I am using the following function:我想每天在某个时间向用户发送通知,为此,我使用以下 function:

Future<void> showDailyAtTime() async {
////////// <<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
var time = Time(16, 02, 00); ////// line 1 <---------- (Hour, Minute, Second)
////////// <<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
var androidChannelSpecifics = AndroidNotificationDetails(
  'CHANNEL_ID 2',
  'CHANNEL_NAME 2',
  "CHANNEL_DESCRIPTION 2",
  importance: Importance.max,
  priority: Priority.high,
);
var iosChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics =
NotificationDetails(android: androidChannelSpecifics, iOS: iosChannelSpecifics);
await flutterLocalNotificationsPlugin.showDailyAtTime(
  0,
  'Test Title at ${time.hour}:${time.minute}.${time.second}',
  'Test Body', //null
  time,
  platformChannelSpecifics,
  payload: 'Test Payload',
 );
}

I am storing the time using shared preferences like this:我使用这样的共享首选项存储时间:

ttime = '${time.hour} : ${time.minute} : ${time.second}';

And then I am setting it like this:然后我像这样设置它:

Future<void> _setNotifyTime() async{
final prefs = await SharedPreferences.getInstance();
final savedNotifyTime = await _getStringFromSharedPrefs();
await prefs.setString('notificationTime', ttime);
//print("this $savedNotifyTime");
return savedNotifyTime;
}

Then I am getting from shared preferences with the help of following code:然后我借助以下代码从共享偏好中获取信息:

Future<String> _getStringFromSharedPrefs() async{
final prefs = await SharedPreferences.getInstance();
notifyTime = prefs.getString('notificationTime');
return notifyTime;
}

How can I convert this saved time to the required format (as shown in line 1), the values are in int?如何将此保存的时间转换为所需的格式(如第 1 行所示),值是 int? How can I do it?我该怎么做? Please help me.请帮我。

Thanks for your replies in advance感谢您提前回复

var splited = notifyTime.split(':');
int hour = int.parse(splited[0]);
int minute = int.parse(splited[1]);
int second = int.parse(splited[2]);

You can do pattern splitting like in the answer by Jorge Vieira or you save yourself the parsing logic and just store it as ints.您可以像Jorge Vieira的回答那样进行模式拆分,或者您自己保存解析逻辑并将其存储为整数。


For setting you can use setInt()对于设置,您可以使用 setInt()

prefs.setInt("H",time.hours);
prefs.setInt("m",time.minutes);
prefs.setInt("s",time.seconds);

For getting it you can use getInt()为了得到它,你可以使用 getInt()

int hours = prefs.getInt("H");
int mins = prefs.getInt("m");
int secs = prefs.getInt("s");

Then from your methods just return the Time object然后从您的方法中返回时间 object

return Time(hours,mins,secs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM