我正在 Flutter 中创建 todo 应用程序。我需要明智地显示 Todos 日期。就像今天创建的所有待办事项都应该显示在今天下一样,所有明天的待办事项都应该显示在明天下。
我像这样创建了我的表:
database.execute("""
CREATE TABLE Todotable(
id INTEGER PRIMARY KEY AUTOINCREMENT,
taskName TEXT NOT NULL,
taskTag TEXT NOT NULL,
date TEXT NOT NULL,
isReminder INTEGER NOT NULL,
isCompleted INTEGER NOT NULL
)
""");
我不知道如何明智地查询 SQFlite 数据日期并将其格式化为 Today 和 Tomorrow。并按照设计中所示的今天和明天的部分显示。
感谢您的回答:)
答案 0 :(得分:1)
获取明天的待办事项
//for tomorrow
String tomorrowDate= DateTime.now().add(Duration(days: 1)).toIso8601String();
var todosForTomrrow= await database
.rawQuery('SELECT * FROM Todotable WHERE date = ?', [tomorrowDate]);
//for today
String todayDate= DateTime.now().toIso8601String();
var todosForToday= await database
.rawQuery('SELECT * FROM Todotable WHERE date = ?', [todayDate]);
日期被转换并以字符串格式保存在这里,日期在插入表格之前应该转换为相同的格式
答案 1 :(得分:0)
您可以在 sqflite
中创建 DATETIME 列。
以下是在 sqflite 数据库中创建的 Weather
表的示例:
batch.execute('''
CREATE TABLE $tableWeather (
$weatherLocalization TEXT NOT NULL,
$weatherDate DATETIME NOT NULL,
$weatherHumidityPercentage REAL,
$weatherWindDegree REAL,
$weatherWindSpeed REAL,
$weatherPressureHPascal INTEGER,
$weatherTemperatureCelsius REAL,
$weatherDescription TEXT,
$weatherIconId TEXT,
PRIMARY KEY($weatherLocalization, $weatherDate));
''',);
您可以按如下方式创建 Weather
对象:
class Weather{
String localization;
DateTime date;
double humidityPercentage;
double windDegree;
double windSpeedMS;
int pressureHPascal;
double temperatureCelsius;
String description;
String iconId;
Weather({
@required this.localization,
@required this.date,
this.humidityPercentage,
this.windDegree,
this.windSpeedMS,
this.pressureHPascal,
this.temperatureCelsius,
this.description,
this.iconId
});
//to be used when inserting a row in the table
Map<String, dynamic> toMap() {
final map = new Map<String, dynamic>();
map["$weatherLocalization"] = localization;
map["$weatherDate"] = date.toString();
map["$weatherHumidityPercentage"] = humidityPercentage;
map["$weatherWindDegree"] = windDegree;
map["$weatherWindSpeed"] = windSpeedMS;
map["$weatherPressureHPascal"] = pressureHPascal;
map["$weatherTemperatureCelsius"] = temperatureCelsius;
map["$weatherDescription"] = description;
map["$weatherIconId"] = iconId;
return map;
}
//to be used when converting the row into object
factory WeatherOnDate.fromMap(Map<String, dynamic> data) => new WeatherOnDate(
localization: data["$weatherLocalization"],
date: DateTime.parse(data["$weatherDate"]),
humidityPercentage: data["$weatherHumidityPercentage"],
windDegree: data["$weatherWindDegree"],
windSpeedMS: data["$weatherWindSpeed"],
pressureHPascal: data["$weatherPressureHPascal"],
temperatureCelsius: data["$weatherTemperatureCelsius"],
description: data["$weatherDescription"],
iconId: data["$weatherIconId"]
);
}
小心地将您的 DateTime
属性转换为字符串或 int
,就像我在 toMap()
函数中所做的那样。
然后,当您想要获取日期时,您可以这样做:
Future<Weather> fetchWeatherOnDate(DateTime dateTime) async {
DatabaseHelper _databaseHelper = Injection.injector.get();
List<Map<String, dynamic>> weatherMaps = await _databaseHelper.db.rawQuery(
'SELECT * FROM $tableWeather WHERE DATE($weatherDate) = DATE(?)',
[dateTime.toString()]);
List<Weather> weathers= [];
for (final weatherMap in weatherMaps) {
weathers.add(Weather.fromMap(weatherMap));
}
if (weathers.isNotEmpty){
return weathers[0];
}
return null;
}
DateTime today = DateTime.now()
Weather weatherToday = fetchWeatherOnDate(today);
我认为它让您对如何解决问题有了很好的了解:)