The method 'watchPerformance' isn't defined for the type 'IntegrationTestWidgetsFlutterBinding' - flutter

I'm currently doing the integrated_test codelab on https://codelabs.developers.google.com/codelabs/flutter-app-testing/#6.
I'm on step 6 where I have to test the performance of the test_app:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:testing_app_codelab/main.dart';
import 'package:testing_app_codelab/testing_app.dart';
void main() {
group('Testing App Performance Tests', () {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized()
as IntegrationTestWidgetsFlutterBinding;
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
testWidgets('Scrolling test', (tester) async {
await tester.pumpWidget(TestingApp());
final listFinder = find.byType(ListView);
await binding.watchPerformance(() async { // error is here
await tester.fling(listFinder, Offset(0, -500), 10000);
await tester.pumpAndSettle();
await tester.fling(listFinder, Offset(0, 500), 10000);
await tester.pumpAndSettle();
}, reportKey: 'scrolling_summary');
});
});
}
I've gone through the IntegrationTestWidgetsFlutterBinding documentation and watchPerformance() is a method of it(please check: https://api.flutter.dev/flutter/package-integration_test_integration_test/IntegrationTestWidgetsFlutterBinding/watchPerformance.html).
The error I get on VS code is:
The method 'watchPerformance' isn't defined for the type 'IntegrationTestWidgetsFlutterBinding'.
Try correcting the name to the name of an existing method, or defining a method named 'watchPerformance'.
When I run the Integration test on my Android phone I get this error:
Try correcting the name to the name of an existing method, or defining a method named 'watchPerformance'.
await binding.watchPerformance(() async {

I got the idea to solve this problem from this post Flutter / Dart package integration_test with null safety
The problem was that integration_test dependency doesn't support null-safety.
when you check which dependencies have null safety support using this command:
dart pub outdated --mode=null-safety
The output is:
Showing dependencies that are currently not opted in to null-safety.
[✗] indicates versions without null safety support.
[✓] indicates versions opting in to null safety.
Package Name Current Upgradable Resolvable Latest
direct dependencies:
integration_test ✗0.8.1 ✗0.8.1 ✗0.8.1 ✗1.0.2+3
dev_dependencies: all support null safety.
You are already using the newest resolvable versions listed in the 'Resolvable' column.
Newer versions, listed in 'Latest', may not be mutually compatible.
This means that it's legacy and doesn't have support.
To solve this I changed the pubspec.yaml from this:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
provider: ^6.0.1
test: ^1.19.5
integration_test: ^0.8.1
dev_dependencies:
flutter_test:
sdk: flutter
Changed to this instead:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
provider: ^6.0.1
test: ^1.19.5
dev_dependencies:
integration_test:
sdk: flutter
flutter_test:
sdk: flutter
After this, I ran flutter pub get on the terminal and the error disappeared.
Now when I hover over the watchPerformance() method I get this info:
Future<void> watchPerformance(Future<void> Function() action, {String reportKey = 'performance'})
package:integration_test/integration_test.dart
Watches the [FrameTiming] during action and report it to the binding with key reportKey.
This can be used to implement performance tests previously using [traceAction] and [TimelineSummary] from [flutter_driver]

This is a general programming error that will occur. I will put an answer in because the error description for these is often rubbish at telling you what is going on here.
The error is telling you the function called watchPerformance on the object IntegrationTestWidgetsFlutterBinding (a var here called binding), does not exist.
Does the function use a captial W instead of lower case? Take another look at the docs and check everything is spelled correctly etc.

Related

package is forbidden but only when it's imported

Here's an error I'm getting when trying to save the pubspec.yaml (which automatically runs flutter pub get) (I changed the names of the packages):
Running "flutter pub get" in ravencoin_front...
Because every version of package_b from path depends on
package_c any which is forbidden, package_b from path is
forbidden. So, because package_a depends on package_b from
path, version solving failed. pub get failed (server unavailable) --
attempting retry 1 in 1 seconds...
So the question is, why is package_c forbidden? Here's what package_c looks like:
pubspec.yaml:
name: package_c
version: 1.0.0
description: Serverpod client for communication to the consent server and database.
homepage: https://github.com/.../package_c
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
serverpod_client: ^0.9.8
/lib/package_c.dart
library package_c;
export 'package:package_c/src/protocol/protocol.dart';
export 'package:serverpod_client/serverpod_client.dart';
Finally here's protocol.dart:
/* AUTOMATICALLY GENERATED CODE DO NOT MODIFY */
/* To generate run: "serverpod generate" */
// ignore_for_file: public_member_api_docs
// ignore_for_file: unnecessary_import
library protocol;
// ignore: unused_import
import 'dart:typed_data';
import 'package:serverpod_client/serverpod_client.dart';
import 'consent_class.dart';
import 'consent_document_class.dart';
export 'consent_class.dart';
export 'consent_document_class.dart';
export 'client.dart';
class Protocol extends SerializationManager {
static final Protocol instance = Protocol();
final Map<String, constructor> _constructors = {};
#override
Map<String, constructor> get constructors => _constructors;
Protocol() {
constructors['Consent'] = (Map<String, dynamic> serialization) =>
Consent.fromSerialization(serialization);
constructors['ConsentDocument'] = (Map<String, dynamic> serialization) =>
ConsentDocument.fromSerialization(serialization);
}
}
There are no errors, not sure why this package is seen as "forbidden."
Now package_b also requires other pacakges and overrides their paths, and they work just fine. so at first I thought it doesn't like the "nested paths" part of this situation, but that's not it, otherwise it would complain about the others too.
I also wondered, maybe the issue is that it has two "library" listed in the files. but I don't think that's it because if I remove the library package_c; it has the same problem.
Maybe it's as simple as this package_c not being correctly setup as a package, but I think it is, I looked up the requirements and it seems to fit from what I found.
Just for good measure let me include some of package_b pubspect.yaml:
name: package_b
description: Ravencoin wallet backend library
version: 1.0.0
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
...
package_d: ^4.0.1
...
package_c: ^1.0.0
dependency_overrides:
# causes error!
package_c:
path: ../../package_c
# works fine!
package_d:
path: ../../package_d
...
Now, Here's the really weird part. remember, package_a imports pacakge_b which imports package_c. Well, package_b works just fine, dart pub get works great. but when I run flutter pub get in package_a, the error at the top occurs. the weird thing is only package_a complains about package_c, package_b is fine with package_c... what?
I guess I should include part of the pubspec.yaml for package_a too, this is the one that causes the error:
name: package_a
description: Ravencoin wallet frontend
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+0
environment:
sdk: '>=2.14.0 <3.0.0'
dependencies:
...
flutter:
sdk: flutter
package_b: ^1.0.0
...
dependency_overrides:
package_b:
path: ../package_b
...
...

how to update flutterfire_web_sdk_version and how to enable 'non-nullable' language feature

When I run my code, then I get this warning:
Flutter Web Bootstrap: Programmatic
WARNING: FlutterFire for Web is explicitly tested against Firebase JS SDK version "8.10.1"
but you are currently specifying "8.6.1" by either the imported Firebase JS SDKs in your web/index.html
file or by providing an override - this may lead to unexpected issues in your application. It is recommended that you change all of the versions of the
Firebase JS SDK version "8.10.1":
If you override the version manually:
change:
<script>window.flutterfire_web_sdk_version = '8.6.1';</script>
to:
<script>window.flutterfire_web_sdk_version = '8.10.1';</script>
If you import the Firebase scripts in index.html, instead allow FlutterFire to manage this for you by removing
any Firebase scripts in your web/index.html file:
e.g. remove: <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
I already tried to find the <script>window.flutterfire_web_sdk_version = '8.6.1';</script> in the index file, but I can't find it. Therefore I couldn't upgrade it.
Does someone know how to fix this warning?
second question:
When I want to use non-nullable, then I get this error:
This requires the 'non-nullable' language feature to be enabled.
I am using sdk: '>=2.6.0 <3.0.0' so it should actually be enabled. Does someone know why it isn't working/enabled?
packages that i am using are (maybe this helps you):
cupertino_icons: ^1.0.2
responsive_builder: ^0.4.2
get_it: ^7.2.0
provider: ^4.1.1
provider_architecture: ^1.1.1+1
firebase_auth: ^3.3.19
firebase_core: ^1.17.1
cloud_firestore: ^3.1.13
I already tried to clean flutter and using Pug get again.
Second question:
For null safety you need to use sdk: '>=2.12.0 <3.0.0'. Here you can find how to enable null safety.

Incomplete import in the generated_plugin_registrant.dart

I have a plugin created using the federated approach. The problem is that the generated_plugin_registrant.dart contains an incomplete import which is pointing to the app-facing package but leaves out the dart file name and this causes an error.
import 'package:my_plugin/'; // PROBLEMATIC LINE
import 'package:my_plugin_web/my_plugin_web.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
// ignore: public_member_api_docs
void registerPlugins(Registrar registrar) {
.registerWith(registrar); // PROBLEMATIC LINE
MyPluginWeb.registerWith(registrar);
registrar.registerMessageHandler();
}
This code is in the default example app for a plugin with an unchanged default pubspec.yaml.
Here's the pubspec.yaml for the app-facing "my_plugin" package:
name: my_plugin
description: A new flutter plugin project.
version: 0.0.1
author:
homepage:
environment:
sdk: '>=2.12.0 <3.0.0'
flutter: '>=1.20.0'
dependencies:
flutter:
sdk: flutter
my_plugin_platform_interface:
path: ../my_plugin_platform_interface
my_plugin_web:
path: ../my_plugin_web
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
plugin:
platforms:
android:
package: com.example.my_plugin
pluginClass: MyPlugin
ios:
pluginClass: MyPlugin
web:
defaultPackage:
path: my_plugin_web
I've tried all the usual commands like flutter clean and recreating the project but to no avail.
Under your web definition (which Google wants in a separate project you have to specify it as
web:
pluginClass: The class name of your web class here
fileName: The file name of the file that holds your web class here
I had it has just "file" and yours is "path" which will cause this.
The issue is that plugin support for the web was not generated for a specific package or packages.
Try creating an analysis_options.dart and add these lines
analyzer:
exclude: [lib/generated_plugin_registrant.dart]
then configure your vscode/.settings.json with these lines
"dart.analysisExcludedFolders":["./yourApp/lib/generated_plugin_registrant.dart" ]
Just find the answer, basically you need to use default_package to specify your are using the integrated plugin. More here

How to access Flutter environment variables from tests?

I've been using flutter_dotenv to load environment variables saved in .env throughout the application and it's worked just fine to date. As I'm trying to write tests though, I cannot seem to access these from a test file.
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
setUp(() async {
await DotEnv().load();
});
test('Incorrect password should be rejected', () {
var password = DotEnv().env['PASSWORD'];
// stuff
});
}
Result of running the test:
Shell: [flutter_dotenv] Load failed: file not found
Shell: [flutter_dotenv] No env values found. Make sure you have called DotEnv.load()
It just can't seem to find the .env file. I even made a copy of .env in the test directory but it didn't recognise that either.
I've tried using Platform.environment instead of flutter_dotenv to access the variable, but that didn't work either, returning null.
Apologies if I'm being silly here, it's my first time writing Flutter tests, but would appreciate advice.
Update:
This is what my pubspec.yaml looks like:
name: //name
description: //description
version: 1.0.0+3
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
font_awesome_flutter: ^8.5.0
flutter_youtube: ^2.0.0
http: ^0.12.0+4
flutter_dotenv: ^2.1.0
google_fonts: ^0.3.7
photo_view: ^0.9.2
flutter_page_transition: ^0.1.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- images/
- .env
Add TestWidgetsFlutterBinding.ensureInitialized(); as the first line of your test's main function.
flutter_dotenv is trying to access ServicesBinding mixin which interacts with your app's platform. You need to initialize this binding first before accessing it and the above line of code will ensure this initialization occurs before running your tests.
Your code should look like this:
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
void main() async {
TestWidgetsFlutterBinding.ensureInitialized();
await DotEnv().load();
setUp(() {
// anything else you need to setup
});
test('Incorrect password should be rejected', () {
var password = DotEnv().env['PASSWORD'];
// stuff
});
}
For those having an error of type FileNotFoundError, update flutter_dotenv. From version 5.0.1 there is new method called testLoad() that works as expected because it doesn't use rootBundle to load the file as an asset.

Flutter | Dart : Target of URI does not exist

I am making my first Application to build an Android App with Flutter.
I am using Android Studio as IDE.
The problem is when I import the http package:
import 'package:http/http.dart' as http;
I get an error :
error: Target of URI doesn't exist: 'package:http/http.dart'.
(uri_does_not_exist at [flutter_crypto] lib\home_page.dart:3)
That's my code :
Future<List> getCurrencies() async{
String cryptoUrl = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
http.Response response = await http.get(cryptoUrl);
return JSON.decode(response.body);
}
Thanks,
You need to add the HTTP dependency to pubspec.yaml as per below.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
http: ^0.12.0
With the dependency added you then need to run the following command to update/install required packages:
flutter packages upgrade
Hope this helps
Make sure you have added the dependency to pubspec.yaml
dependencies:
http: ^0.12.0
You will also need to:
flutter packages get
For any upcoming problem with importing packages from dart please go to official Dart website packages and search for needed package and you will find solution up there.
for your issue go to:
https://pub.dartlang.org/packages/http
Installing tap https://pub.dartlang.org/packages/http#-installing-tab-
follow guide steps:
In pubspec.yaml file:# dependencies
dependencies:
http: ^0.12.0
flutter:
(NB: please make sure that http & flutter or any other attribute inside dependencies are aligned as above and they are at same line)
In terminal run below command:
$flutter packages get
run: dart pub --trace get --no-precompile in android srudio Terminal