颤振:DateTime.now不反映手动设置的时区

时间:2019-06-28 05:10:24

标签: datetime flutter dart

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

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

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

  

使用在本地时区中的当前日期和时间构造一个DateTime实例。

还有另一种获取电话时间的方法吗?


Flutter Doctor

[√] 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(),
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

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