datetime - Flutter:DateTime.now 不反射(reflect)手动设置的时

为了测试不同时区的效果,我手动更改了日期和时间设置以及手机的时区。但是 DateTime.now() 总是返回 CEST 中的时间。

我的手机已连接 Wifi 但未登录移动网络。

这是预期的行为吗?我不确定下面的 local 指的是什么:

Constructs a DateTime instance with current date and time in the local time zone.

还有其他方法可以获取通话时间吗?


flutter 医生

[√] Flutter (Channel stable, v1.5.4-hotfix.2, on Microsoft Windows, locale en-US)
    • Flutter version 1.5.4-hotfix.2 at <path>\flutter
    • Framework revision 7a4c33425d (8 weeks ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at <path>\android-sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: <path>\android-studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.4)
    • Android Studio at <path>\android-studio
    • Flutter plugin version 34.0.2
    • Dart plugin version 183.5901
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[√] VS Code (version 1.35.1)
    • VS Code at <path>\VSCode\code.exe
    • Flutter extension version 3.1.0

[√] Connected device (1 available)
    • Wileyfox Swift • b6d5f2fa • android-arm64 • Android 7.1.2 (API 25)

• No issues found!

示例应用

import 'dart:async';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class DateTimeWidget extends StatefulWidget {
  DateTimeWidget({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _DateTimeWidgetState();
}

class _DateTimeWidgetState extends State<DateTimeWidget> {
  DateTime time;
  Timer updateTimer;

  _DateTimeWidgetState() : time = DateTime.now();

  void timerFunction(Timer timer) {
    setState(() {
       this.time = DateTime.now();
    });
  }

  @override
  void initState() {
    super.initState();
    this.updateTimer = Timer.periodic(Duration(seconds: 1), this.timerFunction);
  }

  @override
  void dispose() {
    this.updateTimer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Text("${this.time.toString()}");
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Date Time',
      home: Scaffold(
        body: Container(
          child: Center(
            child: DateTimeWidget(),
          ),
        ),
      ),
    );
  }
}

最佳答案

通过 Android 11 设备在您的代码上使用 Flutter 2.0,即使发生更改,该应用程序也能正确反射(reflect)设备时间。如果问题仍然存在,可能是错误,最好在此处发布 https://github.com/flutter/flutter/issues/

关于datetime - Flutter:DateTime.now 不反射(reflect)手动设置的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56801203/

相关文章:

python - 如何在 Python 中使用计时器解锁条件?

python - 如何序列化 CSR 矩阵

node.js - GCP 应用引擎 : Random pending requests for s

rust-cargo - 如何修复 cargo 的 "failed to parse manifes

python-3.x - 如何在 python 中使用 opencv 读取数据矩阵代码?

python - 如何在 python 中为 for 循环手动输入?

c++ - 使用 Microsoft Visual Studio 运行 C++ "hello wor

swagger - 如何在开放式 API 规范中为字符串类型的属性指定空字符串值作为默认值

pyspark - 如何将 FPGrowth 项集限制为 2 或 3

html - 仅适用于移动设备的引导轮播