我如何将机器人的起始或结束日期和时间存储到Firebase数据库中?

发布于 2025-01-21 16:47:47 字数 666 浏览 2 评论 0原文

我们正在实施轮式移动机器人 对于用于移动应用程序的室内映射,我如何将机器人的启动或终止日期和时间存储到Firebase数据库中?

Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Turn On/Off The Robot"),
  ),
  body: Center(
    child: Container(
      child: FlutterSwitch(
        width: 125.0,
        height: 55.0,
        valueFontSize: 25.0,
        toggleSize: 45.0,
        value: status,
        borderRadius: 30.0,
        padding: 8.0,
        showOnOff: true,
        onToggle: (val) {
          setState(() {
            status = val;
          });
        },
      ),
    ),
  ),
);

例如,这是我要通过打开或关闭开关按钮,然后存储在实时数据库中来进行日期的操作

we are implementing a Wheeled mobile robot
for indoor mapping using flutter for the mobile application, How can I store the starting or ending date and time for my robot into the firebase database in real-time?

Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Turn On/Off The Robot"),
  ),
  body: Center(
    child: Container(
      child: FlutterSwitch(
        width: 125.0,
        height: 55.0,
        valueFontSize: 25.0,
        toggleSize: 45.0,
        value: status,
        borderRadius: 30.0,
        padding: 8.0,
        showOnOff: true,
        onToggle: (val) {
          setState(() {
            status = val;
          });
        },
      ),
    ),
  ),
);

for example, this is the operation that I want to take its date by turning on or turning off the switch button, and then store in a real-time database

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2025-01-28 16:47:47

如果您使用的是firebase firestore,则cloud_firestore软件包具有“时间戳”类,可以将其转换为Datitime对象。如果您想在数据库中创建基于服务器的时间戳,请使用:

"myDateField": FieldValue.serverTimestamp()

这是一个不错的解决方案,但是如果您无法访问时间戳类或希望可以从任何地方访问您的日期,则可以使用<<<强>时期时间戳。在Flutter中,您可以调用.millisecondssepoch获取INT值

int epochTimestamp = myDate.millisecondsSinceEpoch

并创建可以使用的DateTime对象:

DateTime myDate = DateTime.fromMillisecondsSinceEpoch(epochTimestamp)

回答您的问题,您需要在模型中有2个字段:
startDate 端date

/// Get the initial starting time before the route
DateTime startDate = DateTime.now();
///
/// Some logic, where you wait until the end of the route
/// 
/// Get the ending time after all the logic/routing is complete
DateTime endDate = DateTime.now();

If you are using Firebase Firestore, then cloud_firestore package has "Timestamp" class, that can be transformed to/from DatiTime objects. If you want to create a server-based timestamp in your database, use:

"myDateField": FieldValue.serverTimestamp()

This is a nice and easy solution but in case you don't have access to Timestamp class or want your date to be accessible from anywhere you can store it with epoch timestamp. In flutter you can call .millisecondsSinceEpoch to get the int value

int epochTimestamp = myDate.millisecondsSinceEpoch

And to create the DateTime object you can use:

DateTime myDate = DateTime.fromMillisecondsSinceEpoch(epochTimestamp)

Answering your question, you need to have 2 fields in your model:
startDate and endDate for example.

/// Get the initial starting time before the route
DateTime startDate = DateTime.now();
///
/// Some logic, where you wait until the end of the route
/// 
/// Get the ending time after all the logic/routing is complete
DateTime endDate = DateTime.now();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文