gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:21:46 27 4
gpt4 key购买 nike

为了测试不同时区的效果,我手动更改了日期和时间设置以及手机的时区。但是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com