Dart unit tests fail to run on Github Actions - flutter

I have a dart project with unit tests. When I run the unit tests locally they run successfully.
I added CI to the project with Github Actions. The same unit tests that run successfully locally, fail on Github Actions with the following error:
00:00 +0: loading test/foo_test.dart
00:01 +0: loading test/foo_test.dart
00:01 +0 -1: loading test/foo_test.dart [E]
Failed to load "test/foo_test.dart":
Couldn't resolve the package 'foo' in 'package:foo/foo.dart'.
test/foo_test.dart:5:8: Error: Not found: 'package:foo/foo.dart'
import 'package:foo/foo.dart';
^
test/foo_test.dart:33:26: Error: Method not found: 'Bar'.
final bar = Bar(
^^^^^^^^^^^^
00:01 +0 -1: Some tests failed.
Error: Process completed with exit code 1.
foo_test.dart imports foo.dart as a package (not relative path).
import 'package:foo/foo.dart'

The issue was caused due to me renaming the package name in pubspec.yaml.
pubspec.yaml:
name: foo_bar # <---- this was renamed from 'foo' to 'foo_bar'
description: An example dart project
version: 1.0.0
...
Any changes to pubspec.yaml requires running flutter pub get for the changes to take effect - including changing the package name, version, etc.

Related

Flutter - Asset images in package dependencies

We have the following structure:
Package A is a flutter app, It has it's assets folder and is loading assets like this: Image.asset("assets/images/background.png"). project B, that uses package A as dependency.
When I run package A from Project B, some widget of package A have image can not show. Error: Another exception was thrown: Unable to load asset: assets/images/light_theme_background.svg
When I added the "package" field to the AssetImage in the package A, Error is fixed when I run package A from project B, however the package A couldn't read its own contents.
const AssetImage('assets/heart.png', package: 'my_packages');
Is there a way to package A when it can read assets on run from itself and run from the project that takes it as a dependency?
Error when run package A: "Another exception was thrown: Unable to load assets: package/my_packages/assets/images/background.png"
In the documentation it says: "Content used by the package itself should also be fetched using the package argument as above."
I tried copying assets folder to lib folder and also declaring in pubspec.yaml file like the answer here. However, most reported "Bad state: Invalid SVG data " and "Exception: Invalid image data" errors even though they are all normal.

Can't release app or build web because of this package: blue_thermal_printer: ^1.1.9

When I run this command
flutter run --release
flutter build web --web-renderer html
I get this error in console:
Launching lib\main.dart on Infinix X656 in release mode...
/C:/Users/Bilal%20Saeed/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/blue_thermal_printer-1.1.9/lib/blue_thermal_printer.dart:34:28: Error: Type 'Registrar' not
found.
static void registerWith(Registrar registrar) {
^^^^^^^^^
/C:/Users/Bilal%20Saeed/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/blue_thermal_printer-1.1.9/lib/blue_thermal_printer.dart:34:28: Error: 'Registrar' isn't a
type.
static void registerWith(Registrar registrar) {
^^^^^^^^^
/C:/Users/Bilal%20Saeed/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/blue_thermal_printer-1.1.9/lib/blue_thermal_printer.dart:35:24: Error: Setter not found: 'i
nstance'.
BlueThermalPrinter.instance = BlueThermalPrinter._();
^^^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'D:\All Data\Softwares\Latest Flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1005
* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command 'D:\All Data\Softwares\Latest Flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 3m 57s
Running Gradle task 'assembleRelease'... 240.3s
Exception: Gradle task assembleRelease failed with exit code 1
This file is also automatically generated with errors:
//
// Generated file. Do not edit.
//
// ignore_for_file: directives_ordering
// ignore_for_file: lines_longer_than_80_chars
import 'package:blue_thermal_printer/';
import 'package:cloud_firestore_web/cloud_firestore_web.dart';
import 'package:cloud_functions_web/cloud_functions_web.dart';
import 'package:connectivity_for_web/connectivity_for_web.dart';
import 'package:firebase_analytics_web/firebase_analytics_web.dart';
import 'package:firebase_auth_web/firebase_auth_web.dart';
import 'package:firebase_core_web/firebase_core_web.dart';
import 'package:firebase_messaging_web/firebase_messaging_web.dart';
import 'package:firebase_storage_web/firebase_storage_web.dart';
import 'package:fluttertoast/fluttertoast_web.dart';
import 'package:geolocator_web/geolocator_web.dart';
import 'package:google_maps_flutter_web/google_maps_flutter_web.dart';
import 'package:google_sign_in_web/google_sign_in_web.dart';
import 'package:image_picker_for_web/image_picker_for_web.dart';
import 'package:maps_launcher/maps_launcher_web.dart';
import 'package:shared_preferences_web/shared_preferences_web.dart';
import 'package:url_launcher_web/url_launcher_web.dart';
import 'package:video_player_web/video_player_web.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
// ignore: public_member_api_docs
void registerPlugins(Registrar registrar) {
BlueThermalPrinterPlugin.registerWith(registrar);
FirebaseFirestoreWeb.registerWith(registrar);
FirebaseFunctionsWeb.registerWith(registrar);
ConnectivityPlugin.registerWith(registrar);
FirebaseAnalyticsWeb.registerWith(registrar);
FirebaseAuthWeb.registerWith(registrar);
FirebaseCoreWeb.registerWith(registrar);
FirebaseMessagingWeb.registerWith(registrar);
FirebaseStorageWeb.registerWith(registrar);
FluttertoastWebPlugin.registerWith(registrar);
GeolocatorPlugin.registerWith(registrar);
GoogleMapsPlugin.registerWith(registrar);
GoogleSignInPlugin.registerWith(registrar);
ImagePickerPlugin.registerWith(registrar);
MapsLauncherWeb.registerWith(registrar);
SharedPreferencesPlugin.registerWith(registrar);
UrlLauncherPlugin.registerWith(registrar);
VideoPlayerPlugin.registerWith(registrar);
registrar.registerMessageHandler();
}
As well I constantly see these errors in dart analysis :
error: Target of URI doesn't exist: 'package:blue_thermal_printer/'. (uri_does_not_exist at [food_delivery_app] lib\generated_plugin_registrant.dart:8)
error: Undefined name 'BlueThermalPrinterPlugin'. (undefined_identifier at [food_delivery_app] lib\generated_plugin_registrant.dart:31)
However in debug mode, the app works fine.
And when I remove bluetooth_thermal_print package, it works fine as well too.
What could be the issue? How can I release my app with this package?
It seems as if this plugin would not be properly installed.
Make sure the pubspec.yaml has the dependency listed:
dependencies:
blue_thermal_printer: ^any
Then you can let fetch it fetch the files:
flutter packages get
While on the other hand, you're creating a web project
and then install a plugin, which possibly is rather for Android, iOS or HarmonyOS. flutter config --no-enable-web would disable the web support, which is a possible (even quite likely) candidate for the cause of that error message.
And even if some libraries may provide this kind of functionality in a web-browser
any kind of ESC/POS might be easier to handle without all the abstraction layers.
You may want to follow issue #103 ...which only confirms my educated guess.
Heads off to the developer of this package. I contacted him personally by going to his portfolio given on pub.dev and told him the situation. He updated the package from bluetooth_thermal_printer 1.1.9 to 1.2.0 in about minutes and it all worked smoothly once again....

Error while running built value generator

I got an error while trying to use built value generator
This was the error that i got.
[INFO] Running build...
[INFO] Generating SDK summary.
[SEVERE] built value generator:built value on lib/json_parsing.dart:
Bad state: Unexpected diagnostics:
C:\src\flutter\bin\cacheldart-sdk\lib\core\uri.dart:3259:39 - Expected an identifier.
[SEVERE] built_value_generator:built_value on lib/json_parsing.dart:
Bad state: Unexpected diagnostics:
C:\src\Flutter\bin\cache\dart-sdk\lib\core uri.dart:3259:39 - Expected an identifier.
[SEVERE] built_value_generator:built_value on lib/json_parsing.dart:
Bad state: Unexpected diagnostics:
C:\src\flutter\bin\cache\dart-sdk\lib\coreluri.dart:3259:39 - Expected an identifier.
the class which i was using running the built value generator on
import 'package:built_value/built_value.dart';
part 'src/json_parsing.g.dart';
abstract class Article implements Built<Article, ArticleBuilder> {
Article._();
factory Article([void Function(ArticleBuilder) updates]) = _$Article;
}
As mentioned by #Mohammad on the comments, and from this issue, just add this to pubspec.yaml:
dependency_overrides:
analyzer: '0.39.14'
Note, this is a sibling of dependencies and dev_dependencies.
It looks like build_runner version 1.10.1 pinned the version of analyzer to 0.39.14.
Try to update build_runner: build_runner: ^1.10.1 without adding dependency_overrides: and see.

Why doesn't build_runner generate files when serializing JSON in dart/flutter

I'm just trying to generate my files with the same command i stored like 3 months ago (i'm not very good with backend and devops) and now it doesn't generate the files anymore.
It tells me to use the delete command which i don't remember but even using that one my files still won't be generated.
This is what my log looks like, and below you can find the code for a simple class.
One of my guesses would be this has something to do with me changing my github account, since there's the permission issue mentioned.
Im using vs code in windows 10.
log file:
E:\1 Work\flutter_pilot> flutter pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 336ms
[WARNING] Deleted previous snapshot due to missing asset graph.
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 12.5s
[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 787ms
[INFO] Checking for unexpected pre-existing outputs....
[INFO] Found 13 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository.
[SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remove them. These outputs must be removed manually or the build can be run with `--delete-conflicting-outputs`. The outputs are: lib/models/advisory-service-item.g.dart
lib/models/advisory-service.g.dart
lib/models/auth.g.dart
lib/models/contract.g.dart
lib/models/contributor.g.dart
lib/models/exact-cost-values.g.dart
lib/models/exact-cost.g.dart
lib/models/expert.g.dart
lib/models/paginator-info.g.dart
lib/models/person.g.dart
lib/models/project.g.dart
lib/models/system-country.g.dart
lib/models/user.g.dart
pub failed (78)
class file:
import 'package:json_annotation/json_annotation.dart';
part 'organisation.g.dart';
#JsonSerializable()
class Organisation {
final String name;
Organisation({this.name});
factory Organisation.fromJson(Map<String, dynamic> json) => _$OrganisationFromJson(json);
Map<String, dynamic> toJson() => _$OrganisationToJson(this);
}
I had a similar issue, but I am using Android Studio IDE and I have done the following step:
File => Invalidate caches / Restart
Run following command:
flutter clean
flutter pub get
flutter packages pub run build_runner build --delete-conflicting-outputs
About --delete-conflicting-outputs
Assume conflicting outputs in the users package are from previous builds, and skip the user prompt that would usually be provided.
Reference
If you read the error message carefully, you'll see that it's asking you to try adding --delete-conflicting-outputs at the end of your command. So you should try flutter packages pub run build_runner build --delete-conflicting-outputs.
You just run bellow code..
flutter pub run build_runner build --delete-conflicting-outputs
In my case adding part 'file_name.g.dart'; to the top of the file helped.
file_name.dart:
import 'package:json_annotation/json_annotation.dart';
part 'file_name.g.dart';
#JsonSerializable()
class YourClass {
factory YourClass.fromJson(Map<String, dynamic> json) => _$YourClassFromJson(json);
Map<String, dynamic> toJson() => _$YourClassToJson(this);
// ... your class code
}
I think the main reason this happens to most of us, is the possibility that when you ran this command before, you added the generated g.dart file into your source control. this in turn prevents the build_runner from regenerating the previously generated g.dart files, which it does, even if you only need the new g.dart file. now, I understand that this might be hard to follow, but consider this:
Let's say you have a search_entry.dart file.
You ran the command, and generated the search_entry.g.dart file
successfully.
You then proceed to add the generated search_entry.g.dart file to
your source control repository.
You make another file, let's say maybe cached_data_entry.dart.
When you run the command a second time to generate the
cached_data_entry.dart, it will try and delete the
search_entry.g.dart file to regenerate it along with the
cached_data_entry.dart file.
Since the search_entry.g.dart is protected under the source control,
the build runner won't be able to delete it, so, it throws this
error.
Now, what you need to do to fix the current situation, is delete the
previously all the previously generated g.dart files,
(search_entry.g.dart in our example), then add it to the ignored
files in .gitignore file in your source control. then run the
command again, and hopefully, you it will succeed this time.
What you should do in the future, is ignore these files from your
source control right after you generated them.
Now, finally, I hope I've made it as clear as it should be.
I had the same issue. Solved by using the below command
flutter packages pub run build_runner build --delete-conflicting-outputs
changing the part and class to the same name of the file fixed the issue with me
Running "flutter pub upgrade" solved the problem for me.
I had the same issue. At first I generated a myFirstGeneratedFile.g.dart file then I needed another object type to store then when I tried to generate the file using this command =>
flutter packages pub run build_runner clean
I got the error message.
I fixed it by deleting the file that i created before => myFirstGeneratedFile.g.dart then run the command again.
Cheers!
try something like this via terminal:
Delete previous generated files via command: find ./ -name '*.g.dart' -delete
then: flutter packages pub run build_runner watch —delete-conflicting-outputs
Delete .dart_tool folder and run flutter pub get. After this, run the build runner command
In my case , I have declared a dart file named Organisation.dart instead of small letter organisation.dart , refracting this and running the build runner again worked for me.
in my case, I had forgotten to import the mobx_codegen package
In my case the build_runner process was successful but the .g files weren't produced. I had to restart the IDE (vscode) before I start seeing the .g files.
The issue is with package dependency, executing this flutter pub upgrade --major-versions will do the thing.
Add built_value_generator to the pubspec.yaml to dev_dependencies section
https://pub.dev/packages/built_value_generator
You can read the official documentation - https://dart.dev/tools/build_runner
Make sure this targets bit goes into build.yaml and NOT pubspec.yaml.

Why the app produced by flutter build web sometimes doesn't work?

I have 2 issues that only appear when executing flutter build web.
Sometimes flutter build web fails complaining (wrongly) about types that were not compatible (see below).
Sometimes the build process finishes but then the web app doesn't work: doesn't display anything and there are no messages in the console.
The error I mention is something like this:
% flutter build web
Target dart2js failed: Exception: lib/main.dart:24:31:
Error: A value of type 'ApiUsersRepository' can't be assigned to a variable of type 'UsersRepository'.
- 'ApiUsersRepository' is from 'package:my_app/api_users_repo.dart' ('lib/api_users_repo.dart').
- 'UsersRepository' is from 'lib/users_repo.dart'.
final UsersRepository usersRepository = ApiUsersRepository();
^
Error: Compilation failed.
The app is working in iOS and web when developing.
The solution
I changed all imports of my files like:
import 'package:my_app/users_repo.dart';
To:
import 'users_repo.dart';
More Details
Investigating the error about types, I found this issue, where the important part is this comment: after changing every import to relative format it resolves my problem.
So I did that, and it solved the 2 issues, the compilation error, and the runtime error.
for me I had to remove a package that was corrupted. c:\src\flutter.pub-cache\hosted\pub.dartlang.org\localstorage-4.0.0+1 Apparently, a file had become corrupted by me invertly. I removed the package and did a flutter pub get then recompiled and it worked.