UTC string to local DateTime

I'm calling an API which returns "time" in UTC like hh:mm:ssZ and I want to display this as local time instead. Note date is a separate field in the returned JSON.

I have a custom function (pasted below) that converts the string to DateTime and then sets it to local time. It compiles without error, and a test returns a time w/o the "Z" but setting the variable from this custom function doesn't show local time on the test run. It shows "13:00:00Z" still.

I've tried a few different ways, but never does the test run show any time other than UTC.

import 'dart:convert';

import 'dart:math' as math;

import 'package:flutter/material.dart';

import 'package:google_fonts/google_fonts.dart';

import 'package:intl/intl.dart';

import 'package:timeago/timeago.dart' as timeago;

import '/flutter_flow/lat_lng.dart';

import '/flutter_flow/place.dart';

import '/flutter_flow/uploaded_file.dart';

import '/flutter_flow/custom_functions.dart';

import '/backend/backend.dart';

import 'package:cloud_firestore/cloud_firestore.dart';

import '/backend/schema/structs/index.dart';

import '/auth/firebase_auth/auth_util.dart';

DateTime? utcTimetoLocalDateTime(String? time) {

/// MODIFY CODE ONLY BELOW THIS LINE

// Convert time

if (time == null) {

return null;

}

final utcTime = DateFormat('HH:mm:ssZ').parse(time);

final localTime = utcTime.toLocal();

return DateTime(

DateTime.now().year,

DateTime.now().month,

DateTime.now().day,

localTime.hour,

localTime.minute,

localTime.second,

);

/// MODIFY CODE ONLY ABOVE THIS LINE

}

1
2 replies