尝试初始化数据时得到这个。
构建 UserProfile(dirty, state: _UserProfileState#752a9): LateInitializationError: Field '_userData@32329253' has not been initialized 时抛出了以下 LateError 错误。”
这是代码:
late final User _user;
late final DocumentSnapshot _userData;
@override
void initState() {
super.initState();
_initUser();
}
void _initUser() async {
_user = FirebaseAuth.instance.currentUser!;
try {
_userData = await FirebaseFirestore.instance
.collection('users')
.doc(_user.uid)
.get();
} catch (e) {
print("something went wrong");
}
}
Run Code Online (Sandbox Code Playgroud)
构建函数甚至没有运行,因为我试图打印 _user 和 _userData 以检查它们是否已被初始化。
如果我尝试在 initUser() 函数中打印 _user 和 _userData,则会在错误语句之后打印 _user 和 _userData。
请帮助我找到解决此错误的方法。
我正在尝试使用 flutter_local_notifications 包通过我的应用程序推送本地通知,并且与 FlutterNotificationsPlugin 一样,不推荐使用“.schedule”我尝试使用“.zonedSchedule”,但它需要 TZDateTime 值。
var androidDetails = AndroidNotificationDetails(t.id, "Task Notifier",
"Notifies if you have a task scheduled at a particular time");
var generalNotificationDetails =
NotificationDetails(android: androidDetails);
var now = DateTime.now();
var scheduledTime = DateTime(
now.year, now.month, _dayTasks.date.day, t.time.hour, t.time.minute)
.subtract(Duration(minutes: 5));
//use zonedSchedule once you figure out how to convert datetime to TZdatetime
// await notifications.zonedSchedule(
// 0, "Task", t.description, scheduledTime, generalNotificationDetails,
// androidAllowWhileIdle: true,
// uiLocalNotificationDateInterpretation:
// UILocalNotificationDateInterpretation.wallClockTime);
await notifications.schedule(0, "Ideal Day Task", t.description,
scheduledTime, generalNotificationDetails);
Run Code Online (Sandbox Code Playgroud)