3

i need some help because im going crazy. I just want to format date to my locale (should be it-IT).

In flutter app i putted in pubspec.yaml the intl package with the new nullsafety feature:

  flutter:
    sdk: flutter
  intl: ^0.17.0-nullsafety.2 

looking into the documentation it says:

<< Note that before doing any DateTime formatting for a particular locale, you must load the appropriate data by calling: >>

import 'package:intl/date_symbol_data_local.dart';
...
initializeDateFormatting('de_DE', null).then(formatDates); 

(https://pub.dev/packages/intl/versions/0.17.0-nullsafety.2)

so, just to try, i did:

initializeDateFormatting('it-IT', null).then((value) => {print('intl ok')});

but the dart nullsafety doesnt want a null parameter, and it says "The argument type 'Null' can't be assigned to the parameter type 'String'."

Any suggestion?

Francesco
  • 103
  • 2
  • 7
  • 1. `intl` 0.17.0 is out, so there should be no reason to continue using the 0.17.0-nullsafety.2 pre-release version. (Unfortunately, the documentation is still wrong though.) 2. This is unrelated to your reported problem, but `(value) => {print(...)}` is a function that returns a `Set`. You either want `(value) => print(...)` or `(value) { print(...); }`. 3. Have you tried passing an empty string (`''`) instead of `null`? – jamesdlin Aug 22 '21 at 19:41
  • Hi, thanks for your comment. Here the answers: 1. right, now im using intl 0.17.0 but the problem is still there 2. yes i had to do `(value) => print(...)` but it doesnt matter 3. yes i have tried, but then i had a runtime error: `Cannot open file, path = 'symbols/it-IT.json'` – Francesco Aug 23 '21 at 11:47
  • 1
    See https://github.com/dart-lang/intl/issues/417. Are you using `import 'package:intl/date_symbol_data_local.dart';`? – jamesdlin Aug 23 '21 at 20:33

3 Answers3

0

Thread: flutter intl 0.17.0 package initializeDateFormatting.

Same problem here, did you solve the issue? I tried upgrading intl to ^0.18.0, but in my pubspec.yaml I am using

  flutter_localizations: # Add this line
    sdk: flutter

so, my project is coupled with intl 0.17.0. I realize this when I tried the [flutter pub get] command and got this message:

Running "flutter pub get" in expenses...                        
**Because expenses depends on flutter_localizations from sdk which depends on intl 0.17.0, intl 0.17.0 is required.**
So, because expenses depends on intl ^0.18.0, version solving failed.
pub get failed (1; So, because expenses depends on intl ^0.18.0, version solving failed.)

I also made a lab with only dart and not flutter, and intl works as expected: https://gist.github.com/ejyanezp/994c49f15bb2ce3fd4c3d84ed37ca198

So is a compatibility issue between flutter_localizations and intl.

Conclusion: i will go for a workaround and wait the final solution from the authors of both libraries.

0

I also encountered the similar issue where initializeDateFormatting(localeName, null) was imported from 'package:intl/date_symbol_data_file.dart' and it requires filePath in arguments or if you import it from 'package:intl/date_symbol_data_http_request.dart' you have to pass url in second argument that is required.

Intl package has initializeDateFormatting() declared in different three files if you only want to pass locale you should consider it importing from date_symbol_data_local.dart

import 'package:intl/date_symbol_data_local.dart'

final String localeName = Intl.canonicalizedLocale('en_US');

try {
  await initializeDateFormatting(localeName, null);
} catch (error, stackTrace) {
  
}
Mr Sandhu
  • 101
  • 1
  • 6
0

The error message you're seeing is because the initializeDateFormatting function expects a non-null String parameter to specify the locale, but you're passing null. To fix this issue, you should pass the locale as a string, like this:

import 'package:intl/date_symbol_data_local.dart';

void main() async {
  await initializeDateFormatting('it_IT', null);
  print('intl ok');
}

Here, I've used 'it_IT' as the locale identifier for Italian (Italy). Make sure to use an underscore instead of a hyphen when specifying the locale. This should allow you to initialize date formatting for the Italian locale in your Flutter app.