Need help to resolve this issue while running flutter code - flutter

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.

Related

How to restart a Flutter app from the beginning of the main()?

I want to restart my app at some point from the beginning of the main() function and not just rebuild the whole widget tree.
Here is an example of my code, the thing is I want to call init() when restart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await init();
runApp(
const MyApp(),
);
}
I have tried to wrap the widget tree with a Restart widget and tried phoenix package
But both of them just rebuild the widget tree regardless of what is above in main(). I even tried to call the main() directly but it didn't work as expected.
Is there any solution for this case?
I think you can try "Restart App" module
Instal from your teminal:
flutter pub add restart_app
Import from your dart file:
import 'package:restart_app/restart_app.dart';
Call "Restart App" from main():
onPressed: () {
Restart.restartApp();}
Have a nice day...
You runApp(YourApp()) again where you want to restart.1

Flutter App Localization : Arb file not readable error

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

Import js.dart and html.dart for a mixed web / mobile flutter project

I'm using Flutter 2.5.2. I have a project which is to be used for both web and mobile (Android / iOS).
There is one particular widget where I need to use one version of the widget when deploying for web, which uses the JS and HTML packages. When deploying for mobile, I need to use a different version which just uses standard Flutter widgets. (The reasons for this are complex - I'm embedding Unity inside flutter).
So for example, I have this web_player.dart version for web:
import 'dart:html' as html;
import 'package:js/js.dart';
import 'package:flutter/material.dart';
#JS('loadPlayer')
external String loadPlayer();
class WebVersion extends StatelessWidget {
const WebVersion({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
// Use the HTML package and return an HtmlElementView
}
}
And this mobile_player.dart version for mobile:
class MobileVersion extends StatelessWidget {
const MobileVersion({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Text("Mobile version")
}
}
Building for web is fine. The problem is that when I build for mobile, the build breaks with:
Error: Not found: 'dart:js'
and
Error: Not found: 'dart:html'
I'm aware that these packages do not exist in the mobile platform, and I'm trying to find a workaround. I initially tried to conditionally use the WebPlayer or MobilePlayer widgets depending on the kIsWeb flag, but this didn't make any difference (I guess because kIsWeb is not a complile-time constant?)
So then I attempted to work around this using conditional imports. So, I created a stub player_stub.dart:
import 'package:flutter/material.dart';
Widget getPlayer() => throw UnsupportedError("This is a stub");
I import this stub in a wrapper Player widget:
import 'package:mypackage/player_stub.dart'
if (dart.library.io) 'package:mypackage/mobile_player.dart'
if (dart.library.js) 'package:mypackage/web_player.dart';
class Player extends StatelessWidget {
const Player ();
#override
Widget build(BuildContext context) {
return getPlayer();
}
}
And then added stub implementations in my mobile_player.dart:
Widget getPlayer() => MobilePlayer();
and in my web_player.dart:
Widget getPlayer() => WebPlayer();
I was hoping this would resolve the build error by compile time tree shaking or something, but it doesn't.
How do I solve this? I'm totally stumped.
Just to answer my own question for others looking for an answer: the solution was moving the stub, the mobile widget and the web widget to a separate dart library.
The library uses conditional exports like this in it's root my_player_package.dart file:
export 'src/player_stub.dart'
if (dart.library.js) 'src/web_player.dart'
if (dart.library.io) 'src/mobile_player.dart';
So now I reference my separate library in my pubspec.yaml:
dependencies:
...
my_player_package:
path: ../my_player_package
And I can now import 'package:my_player_package/my_player_package.dart' and build for both web and mobile.
I'm attempting to use the js package in a cross-platform app and this was my solution.
1. Create a js_stub.dart file
class JS {
final String? name;
const JS([this.name]);
}
allowInterop<F extends Function>(F f){
throw UnimplementedError();
}
2. Use a conditional import statement
import 'package:pwa_install/js_stub.dart' if (dart.library.js) 'package:js/js.dart';
The rest of my code was left untouched and the app runs as expected on web and mobile.

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.

What is the difference between runApp(new MyApp()) and runApp(new MaterialApp()) in flutter?

In flutter we can pass a stateless widget that returns a MaterialApp instance to the runApp() function like this:
void main()=>runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
...
);
}
}
or we can pass the instance of MaterialApp directly to the runApp() function like so:
void main()=>runApp(
new MaterialApp(
...
);
);
What is the difference between these to ways? Thanks.
There's no difference in visual behavior.
What changes is how hot reload behaves.
For example if you used runApp(MaterialApp()), changing from
runApp(MaterialApp(title: 'Foo'))
to
runApp(MaterialApp(title: 'Bar'))
then the hot reload wouldn't take changes into consideration.
While if you had the following class :
class MyApp {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Foo',
);
)
}
and used it like this :
runApp(MyApp())
then changing title of MyApp would be correctly hot reloaded.
For hot-reload to be able to keep the state, it applies code changes and re-runs build() so that the view is updated. If the whole app would be restarted, the previous state would be lost. This is not desired. If you want this use hot restart instead.
This also means that changes to code that is only executed when the whole app is restarted, will not be applied to the current state.
For more details about hot-reload and limitations see https://flutter.io/docs/development/tools/hot-reload
To add custom behavior on hot-reload the method State<T>.reassemble can be overridden https://docs.flutter.io/flutter/widgets/State/reassemble.html
In one case you have a class, which you can add more methods to, and use them. In one case you don't.