0

Whenever I am trying to convert from TimeOfDay to String using pickedTime.format(context) on the context of TextFormField and Form widgets. It throws an error on released versions (built by flutter build apk or flutter build web) of the android real phone and chrome, but It works fine on Android emulator, Firefox, or Edge.

The error is: Trying to read from 14:35 at position 6.

Just to let you know, yesterday (July, 1st, 2023) I tested with:

  • Window 10
  • Android 10
  • flutter 3.3.10 and all version of the packages related to this flutter SDK, for example Intl 0.17.0, etc.
  • Firefox 114.0.2 (64-bit) => All good.
  • Edge 114.0.1823.67 (Official build) (64-bit) => All good.
  • Chrome 114.0.5735.199 (Official Build) (64-bit) => Error.

Release version tested with Firefox enter image description here

Release version tested with Edge enter image description here

Release version tested with Chrome enter image description here

Let us hope a future update of Google Chrome and Android will solve this issue.

Here is a minimal example to check it out:

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  String? resTime;
  /// get picked time
  /// input params: context and an String DateTime
  /// output: String time picked or current time
  Future<String> pickTime(
      BuildContext context, String paramStringDateTime) async {
    print("Inside pickTime method, paramStringDateTime: $paramStringDateTime");
    String returnTime = DateFormat(
        'HH:mm:ss')
        .format(DateTime.tryParse(paramStringDateTime) ?? DateTime.now());
    print(
        "Inside pickTime method, getting returnTime before showTimePicker: $returnTime");
    TimeOfDay? pickedTime = await showTimePicker(
      initialTime: TimeOfDay.fromDateTime(DateTime.parse(paramStringDateTime)),
      context: context,
//       // These following lines are not mandatory
//       builder: (context, childWidget) {
//         return MediaQuery(
//           // Using 24-Hour format // If you want 12-Hour format, just change alwaysUse24HourFormat to false or remove all the builder argument
//             data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
//             child: childWidget!);
//       },
    );
    print("Inside pickTime method, pickedTime: $pickedTime");
    if (pickedTime != null) {
      // if (!mounted) return returnTime;
      returnTime = DateFormat("HH:mm:ss")
          .format(DateFormat("hh:mm a").parse(pickedTime.format(context)));
      print("Inside pickTime method, returnTime: $returnTime");
    }

    return returnTime;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            GestureDetector(
              onTap: () async {
                resTime = await pickTime(context, (resTime != null ? '2023-01-01 $resTime' : '2023-01-01 14:31:00.000'));
                setState(() {});
              },
              child: Text(resTime ?? "22:30"),
            )
          ],
        ),
      ),
    );
  }
}

Just an update (July 17th, 2023)

I have converted all app for flutter 3.10.6 and the same issue is there, I mean it works on Firefox, Edge, or Android simulator. On the other hand, it is not working on Chrome (Version 114.0.5735.199 Official Build - 64-bit) and Android real device. Here is my flutter doctor output command: enter image description here

The issue is happening on Google Chrome: enter image description here

Alex Correia
  • 658
  • 6
  • 14

0 Answers0