How to use, load and pack flutter assets - flutter

I am fairly new to the dart and flutter. I'd like to make an installer for an android app running on windows by flutter language. To execute adb commands I am using Process.run() with adb platform-tools path which is currently stored on my desktop. I'd like to have this folder stored inside my flutter .exe or somewhere near finalapp.exe when I'll build it with the command flutter build windows so the windows app can still use and access platform-tools without changing the path to it. (I am imagining to work like this: I will create a .exe app, then I will open it on another computer with clean windows install and the app [stored inside the platform-tools] could still be installed by platform-tools stored inside the same .exe) How can I achieve it, please?
Here is my code:
import 'dart:io';
import 'package:fluent_ui/fluent_ui.dart';
import 'installation_and_permissions.dart' as installer;
var stdoutCommand = "", stderrCommand = "", deviceName = "";
Future<void> main() async {
await Process.run(
'adb',
['devices'],
workingDirectory: "assets\\platform-tools",
runInShell: true,
).then((ProcessResult res) {
stdoutCommand = res.stdout;
deviceName = stdoutCommand.substring(26, 34);
stderrCommand = res.stderr;
print(res.stdout);
print(res.stderr);
});
//await installer.installApp();
//await installer.grantPermissions();
runApp(const MyApp());
}
Right now I have my assets (platform-tools and apk I want to install) stored in folder assets and in pubspecs.yaml assets are added:
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets\platform-tools
- assets\platform-tools\app-release-v3.1.01.apk
I googled a lot but I am not a native English speaker and I was unable to find a solution.

Related

Trying to run app on flutter web throws exception [Error: Assertion failed:] [duplicate]

I am using the flutter course "Get to know Firebase for Flutter" from https://firebase.google.com/codelabs/firebase-get-to-know-flutter#4.
I am in step_02 and I have added the following recommended code from stage 5.
import 'package:firebase_auth/firebase_auth.dart'; // new
import 'package:firebase_core/firebase_core.dart'; // new
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart'; // new
import 'firebase_options.dart'; // new
import 'src/authentication.dart'; // new
import 'src/widgets.dart';
Later in this stage there is a Test it section. However it fails because there is no firebase_options.dart file. How do I generate this file.
Thank you.
Previously you had to download the google-service.json and GoogleService-Info.plist from the Firebase console and place them in the android and ios folders in your Flutter app.
Starting with Flutter 2.8, there is a new way to initialize a Firebase project within Flutter to bypass this step.
Create project in Firebase console, but you don't need to download the files mentioned or change build.gradle files
Install Firebase CLI here
run dart pub global activate flutterfire_cli in your Flutter project
run flutterfire configure
This will start a command line interface for you to select the Firebase projects you want to link to the Flutter project. After you complete this, a firebase_options.dart file will be generated in your lib/ folder.
Finally, to initialize Firebase in your main.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());
}
PD: After updating to the new initialization method, Crashlytics and Analytics stopped working for me and I had to revert to the old initialization method. I don't think this new method is quite there yet. Guide for the old method.
The firebase_option file automatically generates after Flutter successfully configures your firebase project with your flutter app. For Android, make sure you've added the google-services.json file to your Android>app root directory and for ios, GoogleService-info.plist file into the root of your Xcode project as well as all targets.
If you're still having problems, I'd suggest using the Firebase CLI direct from the terminal to configure your firebase project.
From your project root terminal, command:
$ flutterfire configure
// This requires the Firebase CLI to work.
Select firebase project by hitting return or enter. Next you'll be asked to select which platforms the configuration should support, e.g. android, ios, web. If you haven't created some of these in the firebase console, don't worry as it will create and register it for you in this step and update the android build.gradle files.
** Proceed to step 4 if you already have the firebase_core plugin installed. **
Install the latest version of the firebase_core plugin by running this command from your project root directory:
$ flutter pub add firebase_core
Add imports to the main file:
import 'package:firebase_core/firebase_core.dart'; //
import 'firebase_options.dart'; // Generated file
Update your main function to initialize firebase with this async function:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options:
DefaultFirebaseOptions.currentPlatform);
runApp(const YourAppName());
}
Delete the google-services.json and google.plist files if you had these installed before.
$ flutter clean
$ flutter run
See the FlutterFire documentation for more information.
After completing the instructions provided by Bugzilla, I was able to locate the firebase_options.dart file in the lib directory. I changed the path of the import from 'firebase_options.dart' to '../firebase_options.dart' and it worked for me.
Solved this by removing my existing Firebase project and creating a new one, disabling Google Analytics.

flutterfire configure --project=whatsappclone-64bbf doesn't generate a lib/firebase_options.dart file in my case

I was connecting my WhatsApp clone using flutter project to firebase. I successfully installed the firebase CLI but i am facing issues while configuring the project. The command flutterfire configure should ideally generate a file named firebase_options.dart file in lib folder but in my case this doesn't seem to happen :(
Install Firebase CLI https://firebase.google.com/docs/cli
run the below commands in your flutter project
dart pub global activate flutterfire_cli
flutterfire configure
After you run these commands, the firebase_options.dart file will be generated in lib/ folder.
add the following lines of code in your main.dart file
make sure to add WidgetsFlutterBinding.ensureInitialized(); above all
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(MyApp());
}

How to set custom image icon on Flutter App? Using Appicon

I Tried changing my app's Icon on apk but it's not changing. I generated an icon on https://appicon.co/ and try changing but it's showing flutter's default app icon.
Try this flutter_launcher_icons
In pubsec.yaml
dev_dependencies:
flutter_launcher_icons: "^0.10.0"
flutter_icons:
android: true
ios: true
image_path: "assets/icon/icon.png"
min_sdk_android: 21 # android min sdk min:16, default 21
And then run this on your terminal (from android studio)
flutter pub run flutter_launcher_icons:main
The command will regenerate icons for android and ios. You can check it in the corresponding folders (eg for android : app/src/main/res/drawable-xxx/ic_launcher)
If you are coming from the future and you have difficulty using the flutter_launcher_icons package.
In July 2022, I tried using the flutter_launcher_icons package (version: "^0.9.2") but it did not work when running the
flutter pub run flutter_launcher_icons:main
and got the famous error 255 code.
After a little search on google, I came across this (very short) and followed this article here: https://www.codewithflutter.com/how-to-change-application-launcher-icon-in-flutter/.
This is what I did to get a launcher icon for the devices that my app would run on.
It's a simple 3 step method to change the app launcher icon.
Step 1:
Go to https://appicon.co/ and use select your launcher's image file.
For best results, it should be 1024 x 1024 pixels.
Select the devices for which you would like the app icon to be generated for. Click on the "Generate" button and a zip file would be downloaded.
When you open the downloaded zip file, it would normally contain the "android" and the "Assets.xcassets" folders (assuming you are generating for both android and ios devices).
Step 2:
Changing the icons manually.
This process is just copying and pasting the folders.
For android:
Open the AppIcons\Android folder and copy all the sub folders.
Then go the android\app\src\main\res folder and paste it (replace the existing file to new files).
For ios:
Open the AppIcons folder and copy the Assets.xcassets folder.
Then go the ios/Runner and paste it (replace the existing file to new files).
Step 3:
Run the command flutter run
Thank you!

Google Fonts package Not working in flutter

The google_fonts package is not working in the final build apk in FLutter, it works fine in the debug mode, but as soon as I build it and install the final apk, It just shows regular font.
It could be because of internet permission issue, google fonts using internet acces and if don't apply the permission in your project it won't work when you install the app.
for this you need to add follow line to file AndroidManifest.xml which is there;
yourappname/android/app/src/debug/AndroidManifest.xml
and the line you will add:
<uses-permission android:name="android.permission.INTERNET"/>
I don't believe it is an internet problem. Most times fonts using Flutter packages contain an error in the Pubspec file.
Check your Pubspec.yaml file. Make sure your google fonts package is Formatted this way
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
google_fonts: ^1.1.0
flutter:
assets:
- google_fonts/ # you only need this line and nothing in the fonts area per https://pub.dev/packages/google_fonts
Make sure you have added the below line in /android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
hello my friend try to check the version first ;)
enter image description here
To use google_fonts you must initialize the license for it.
To do that follow the steps:
Suppose you using this:
fontFamily: GoogleFonts.shadowsIntoLight().fontFamily,
Therefore, you are using shadowsIntoLight font.
To license it, first download the particular font from Google Fonts.
After you download the zip files, extract it.
Make a folder named google_fonts in the root directory of your flutter app file.
=> Now place the .ttf and OFL.txt files (present in the extracted folder) in google_fonts folder that you made previously.
[Note: Only OFL.txt is enough for all your fonts.]
This is how my one looks:
Now go to main.dart file:
=> Add these lines inside the void main() {runApp(MyApp());}:
void main() {
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('google_fonts/OFL.txt');
yield LicenseEntryWithLineBreaks(['google_fonts'], license);
});
runApp(...);
}
and import the followings:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Now close your app and restart it and the release it:
flutter build apk --release
Google fonts uses internet, so you may have to update the dependencies in the manifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Else you can try to make the google font available in the offline by adding it in the assets folder.
And then change the code from
// Online Code
Text(
'This is hammersmithOne from Google Font'
style: GoogleFonts.hammersmithOne(),
),
to
// Offline Code
Text(
'This is hammersmithOne from Google Font',
style: TextStyle(fontFamily: 'hammersmithOne') // This is loaded from assets
),
For futher reference refer this article.

How to do integration testing in Flutter?

I want to do integration testing in Flutter. The tutorial I follow gives the following procedure:
Add the flutter_driver package to pubspec:
dev_dependencies:
flutter_driver:
sdk: flutter
Enable the Flutter driver extension and add a call to
the enableFlutterDriverExtension() function in main.dart.
Run the integration test by using the flutter drive command:
flutter drive --target=my_app/test_driver/my_test.dart
My problem is with understanding step 2. It's not clear to me where in Android Studio do you enable the driver extension and where exactly in main.dart do you call the function enableFlutterDriveExtension().
I also have problems with the third step. After running the said command, it says in my terminal that
Error: The Flutter directory is not a clone of the GitHub project.
The flutter tool requires Git in order to operate properly;
to set up Flutter, run the following command:
git clone -b stable https://github.com/flutter/flutter.git
You have to add this code inside the test_driver/app.dart file.
import 'package:flutter_driver/driver_extension.dart';
import 'package:[YOUR_APP]/main.dart' as app;
void main() {
// This line enables the extension
enableFlutterDriverExtension();
// Call the `main()` function of your app or call `runApp` with any widget you
// are interested in testing.
app.main();
}
You can find more info on the official Flutter documentation site (Steps 3 & 4):
https://flutter.dev/docs/cookbook/testing/integration/introduction
Good luck ;)
In order to run integration test in flutter, you need to create "test_driver" directory inside app root dir. Than you need to create two files inside "test_driver" folder.
Lets call first file "app.dart" and there you need to instrument your app(answer above).
Than you need to create your test file, which needs to be called "app_test.dart" and here you write your actual test code.
When you want to run that test, just run "flutter drive --target=test_driver/app.dart".
About step 3 in your question, check if you set flutter home properly and after adding flutter_driver dependency, run "packages get".