Flutter App Localization : Arb file not readable error - flutter

I am trying to make my app support localization. I want my app to work in 2 languages: English & Hindi. So i added the following package in my pubspec.yaml:
flutter_localizations:
sdk: flutter
intl: ^0.17.0 # Add this line
Then i made a l10n.yaml file and added this:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
I also made the app_en.arb file like this:
{
"helloWorld": "Hello World!",
"#helloWorld": {
"description": "The conventional newborn programmer greeting"
}
}
When i am running this code, it gives me an error:
Generating synthetic localizations package failed with 1 error:
Exception: The 'template-arb-file', LocalFile: 'C:\Users\Crosslynx25\Desktop\SW_Mobile_Platform\lib/l10n\app_en.arb', is not readable.
Please ensure that the user has read permissions.
main.dart file
import 'package:ble_app_flutter/screens/home_screen.dart';
import 'package:ble_app_flutter/screens/otp_screen.dart';
import 'package:ble_app_flutter/screens/splash_screen.dart';
import 'package:ble_app_flutter/utils/colors.dart';
import 'package:ble_app_flutter/l10n/L10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:lottie/lottie.dart';
import 'country_codes.dart'
as CountryCodes;
import 'screens/driver/driver_home_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
State < MyApp > createState() => _MyAppState();
}
Map < int, Color > myTheme = {
50: Color.fromRGBO(9, 65, 155, .1),
100: Color.fromRGBO(9, 65, 155, .2),
};
class _MyAppState extends State < MyApp > {
// This widget is the root of your application.
MaterialColor myColor = MaterialColor(0xFF09419b, myTheme);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BLE App gkhglkjhkl',
debugShowCheckedModeBanner: false,
supportedLocales: L10n.all,
// localizationsDelegates: [
// AppLocalizations.delegate, // Add this line
// GlobalMaterialLocalizations.delegate,
// GlobalWidgetsLocalizations.delegate,
// GlobalCupertinoLocalizations.delegate,
// ],
theme: ThemeData(
primarySwatch: myColor,
),
home: new Login(),
);
}
}
class Login extends StatefulWidget {
#override
State < Login > createState() => _LoginState();
}
class _LoginState extends State < Login > {
// const Login({
var selectedCountry = "91";
var phoneNumber = "";
#override
Widget build(BuildContext context) {
.
. //some code
.
}
}

I got the same issue because I copied a yaml file to create the "app_en.arb" file. I think the issue happened when I renamed it and then the extension .yaml wasn't display. I was seeing "app_en.arb" but in reality it was "app_en.arb.yaml"... Hope this will help some of you.

The error is showing here
C:\Users\Crosslynx25\Desktop\SW_Mobile_Platform\lib/l10n\app_en.arb
You are on Windows so you should use \ and not / in path. So I think modifying your l10n.yaml like this
arb-dir: lib\l10n
will do the trick.

This happened with me, then I discovered that the file is actually app_en.arb (with a space before the name). This caused it not to be found.
You can test it by copying the written path in the log, and see if Windows can find the file.
An issue has been created for the misleading message in here.

I don't know why this caused after copy from pub.dev (app_en.arb ) but if you start to refactor --> rename, you can see your file has one empty space after the file format Look at the empty space
enter image description here

I deleted the file l10n.yaml then I ran the command "flutter gen-l10n" that fixed my problem.
it generated a flutter_gen folder in the .dart_tool.
then as soon as your folder is created, re-create the file l10n.yaml and execute the command flutter gen-l10n normally it fixes the problem

In my case, it happened after I implemented localization with Flutter Intl instead of raw l10n with app_en.arb file. So I had intl_en.arb instead of app_en.arb after I removed app_en.arb.
Solution is to remove l10n.yaml file from project folder if you do not need to use app_en.arb file any more. Flutter Intl is definitly more user friendly and easy to implement.

use this command flutter pub global run intl_utils:generate instead of f̶l̶u̶t̶t̶e̶r̶ ̶g̶e̶n̶-̶l̶1̶0̶n

Related

Firebase makes Flutter App build crashes on it's own library code

I am trying to build an app with firebase services, and try these steps many times, including in a fresh new project just to test, but I think there's a problem with the library:
I am running
flutterfire configure
By terminal on the root folder of the project, then selecting one of the projects, selecting all the available platforms ( android, ios, macos, web), then agreeing with the update of android/build.gradle and android/app/build.gradle
After that I've got :
Firebase configuration file lib/firebase_options.dart generated successfully with the following Firebase apps:
But just importing the libraries make the build crashes
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
The error I'm getting is this
: Error: '$throw' isn't a type.
../…/method_channel/method_channel_firebase.dart:120
$throw coreNotInitialized();
^^^^^^
: Error: Expected ';' after this.
../…/method_channel/method_channel_firebase.dart:120
$throw coreNotInitialized();
^^^^^^^^^^^^^^^^^^
: Error: Expected an identifier, but got ')'.
../…/method_channel/method_channel_firebase.dart:120
Try inserting an identifier before ')'.
$throw coreNotInitialized();
^
The sample App is just this:
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: const Text("My App")),
body: const Center(child: Text("MyApp")),
),
);
}
}
I found this workaround, I think there's a problem with the current library version or flutterfire but the next steps work for me:
I changed the versions of the firebase libaries for previous versions I had work with, run flutter clean and deleted the Podfile.lock and it now it's working
NOT WORKING VERSIONS
firebase_core: ^2.2.0
firebase_messaging: ^14.2.1
WORKING VERSIONS
firebase_core: ^1.1.0
firebase_messaging: ^9.1.3

Flutter (Unable to load asset)

I'm unable to load images to my app although I tried many solutions
my code is
import 'package:flutter/material.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(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.amber,
appBar: AppBar(
backgroundColor: Colors.amber[900],
title: const Text("hi"),
),
body: Image(image: AssetImage("images/ab.png"),
),
),
);
}
}
this is my pubspec.yaml
flutter doctor
1- I used $ flutter clean and then $ flutter pub get.
2- I updated flutter SDK.
3- I mention only the folder (images).
4- I used a smaller image.
5- I restarted my laptop.
6- I run my code in another IDE.
but nothing is working for me, so can anyone help me?
thanks in advance
I copied your main.dart code, I created a images folder with a .png file.
I think the problem could be the position of the images folder, you should place it beside the lib folder.
Without seeing the project folder structure I cannot be sure, but if you do it like that your code should work fine.
Here's the pubspec.yaml:
flutter:
assets:
- images/
Here's a screenshot of the your code running:

Flutter: How can I get list of device locales before building starts?

I want to internationalize my application. To ease translations, I want to use i18n_extension package.
I need to get the list of device locales (any device, even web) before any widget building process starts.
This is required to initially set the correct locale (which can be taken from a server) both for I18n and for Localizations (in WidgetsApp).
The only way I found to get the list of device locales is:
MaterialApp(
localeListResolutionCallback: (deviceLocales, appLocales) => myLocaleResolution(deviceLocales, appLocales),
locale: thisAppsLocale,
home: I18n(
child: MyHomePage(),
initialLocale: thisAppsLocale, ),
Method myLocaleResolution sets thisAppsLocale. Method is invoked once at startup and when the user changes the device locale. So thisAppsLocale is only available after the first build process and cannot be used to set locale and initialLocale. Using setState() within myLocaleResolution throws an exception on startup invocation.
Use window.locales:
import 'dart:ui';
import 'package:flutter/material.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
locale: window.locale,
supportedLocales: window.locales,
home: Scaffold(),
);
}
}
The window singleton gives you access to the Window class.

Need help to resolve this issue while running flutter code

This is the error I'm facing while running flutter programs:
lib/main.dart: Warning: Interpreting this as package URI,
'package:flutter_guide_1/main.dart'.
My code:
import 'package:flutter/material.dart';
void mian() {}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: Text('Hello!'),
);
}
}
you need to return runApp
void main() => runApp(MyApp());
At the top of Android Studio, there is a drop-down menu with targets. It probably says "main.dart".
Inside the box, left of the "main.dart", there is a blue symbol. Either it is an "[ (F) main.dart ]" (the Flutter logo) or it is a Dart-arrow fin logo "[ (X) main.dart ]". It should be the "F" version to build properly, so select that.

What is the proper code style for relative imports in Dart?

I am new to Flutter and Dart. How should I import files found on the same folder and those from other folders? Is it proper to use ../ or should I write with the package url?
import 'package:flutter/material.dart';
import 'package:my_app/core/widgets/menus.dart' show SidebarMenu;
import './settings.dart' show SettingsScreen;
class DashboardScreen extends StatefulWidget {
DashboardScreen({Key key, this.title}) : super(key: key);
final String title;
static const String routeName = '/dashboard';
#override
_DashboardState createState() => _DashboardState();
}
class _DashboardState extends State<DashboardScreen> {
#override
Widget build(BuildContext ctx) {
return Scaffold(
body: landscape(),
);
}
Widget landscape() {
return new SafeArea(
child: Container(
child: new Row(
children: <Widget>[
new Expanded(
child: new SidebarMenu(),
),
],
),
),
);
}
}
The Dart style guide recommends using relative imports.
It has the advantage of staying correct even if you rename your package (which is admittedly rare). It's also short and makes it very clear to the reader that this is a library in the same package.
There have been issues when libraries inside the lib/ directory of a package are referenced using a file path. Currently, if your entry point is in the lib/ dir, which Flutter uses, the Dart compiler will recognize that and change the entry point URI to a package: URI. That avoids most of the issues that was attempted fixed by using full package: URIs. You should still not refer to a file inside the lib/ directory with a path containing lib/ in any other place (like from the bin/ or test/ directories of the pub package).
If you watch the Flutter Boring Show on youtube (sorry, cannot remember the episode, they mention the existence of a bug where use of the relative path (../) can cause issues when used in combination with the package path.
While this bug may have been resolved, they do mention a few times that the package url is the preferred style.