Used SharedPreference throwing error in Unit test. In one function I have used SharedPreference and during it's unit testing getting error as "Binding has not yet been initialized" and many other errors too.
Working unit test:
After uncommenting SharedPreference code:
Below is the code of my User.dart file
import 'package:shared_preferences/shared_preferences.dart';
class User {
final String name;
User(this.name);
Future<bool> userName() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("name", name);
return true;
}
}
and below is the code of test file (User_test.dart)
import 'dart:math';
import 'package:flutter_test/flutter_test.dart';
import 'package:unit_testf/user.dart';
void main() {
test("a unit test", () async {
User user = User("flutter");
final result = await user.userName();
expect(result, true);
});
}
Below is the errors I am getting.
Binding has not yet been initialized.
The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter.
package:flutter/src/foundation/binding.dart 284:9 BindingBase.checkInstance.<fn>
package:flutter/src/foundation/binding.dart 366:6 BindingBase.checkInstance
package:flutter/src/services/binding.dart 54:54 ServicesBinding.instance
package:flutter/src/services/platform_channel.dart 166:45 BasicMessageChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 180:38 BasicMessageChannel.send
package:shared_preferences_foundation/messages.g.dart 115:52 UserDefaultsApi.getAll
package:shared_preferences_foundation/shared_preferences_foundation.dart 46:53 SharedPreferencesFoundation.getAll
package:shared_preferences/shared_preferences.dart 164:57 SharedPreferences._getSharedPreferencesMap
package:shared_preferences/shared_preferences.dart 33:19 SharedPreferences.getInstance
package:unit_testf/user.dart 8:55 User.userName
test/user_test.dart 9:31 main.<fn>
Below is the output of my flutter doctor command
Last login: Thu Feb 16 09:58:26 on console
anandsuthar#anands-MacBook-Air ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.1 22C65 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.75.1)
[✓] Connected device (2 available)
[✓] HTTP Host Availability
• No issues found!
anandsuthar#anands-MacBook-Air ~ %
Based on context you are running you have to be sure that ServicesBinding has been initialized.
Making something which start with showing an Widget as the first thing is easily call the rest as ServicesBinding has been initialized.
But calling method/classes that have to been initialized yet you must be sure of that initialized by calling those two methods which explicitly invoking the initialize.
TestWidgetsFlutterBinding.ensureInitialized();
Or
WidgetsFlutterBinding.ensureInitialized();
So your main method have to be:
void main() async {
// First call
WidgetsFlutterBinding.ensureInitialized();
// Then Your code after
//……
//……
//…...
}
Related
I'm using firebase and shared_preference in my flutter project, where I need to store a incoming message to the shared preferences. Whenever a message is received, I am getting the below exception
I/flutter (29300): FlutterFire Messaging: An error occurred in your background messaging handler:
I/flutter (29300): MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
I know that _firebaseMessagingBackgroundHandler will spawn a new isolate, but does that make other plugins unaccessible??
This is my handler
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final prefs = await SharedPreferences.getInstance();
await prefs.reload();
await prefs.setStringList(
DateTime.now().toIso8601String().substring(0, 19) + ".000000", [message.notification!.body.toString(), message.notification!.title.toString()]);
}
I have also confirmed that shared preferences present in generated_plugin_registrant.dart. I have used FlutterFire CLI for integerating firebase with my app.
My app was working normally since today.I already included Firebase Storage in my Android Flutter App and it works after that today I get AppCheck Errors suddenly. I was not include App Check for our project or not enforced in settings. After that I was following the official documentation for initialization appcheck : https://firebase.flutter.dev/docs/app-check/usage.
This is my Kotlin MainActivity:
import android.os.Bundle
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
FirebaseApp.initializeApp(/*context=*/ this);
val firebaseAppCheck = FirebaseAppCheck.getInstance()
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance())
super.onCreate(savedInstanceState)
}
}
and this is my main():
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate();
runApp(MyApp());
}
I also added this to my app/build.gradle
dependencies {
implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta01'
}
When I make a request to firebase storage, I would expect something like this in my console:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
Instead, I'm getting an error:
2021-11-21 18:11:51.442 2091-3452/com.sekspir.grind4gain W/ExponenentialBackoff: network unavailable, sleeping.
2021-11-21 18:11:53.500 2091-3452/com.sekspir.grind4gain W/StorageUtil: Error getting App Check token; using placeholder token instead. Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: App attestation failed.
2021-11-21 18:12:11.136 2091-3633/com.sekspir.grind4gain V/NativeCrypto: SSL handshake aborted: ssl=0xdaa42da8: I/O error during system call, Connection reset by peer
Did I miss something here? I am using a real Android device with flutter debug build.
This is Firestore AppCheck Stats looks both of request income
But in Storage session there are not any request fail or success.
Can you double check that you correctly enabled the enforcement ?
It is required to work with Firebase Storage solutions.
Are you using your app in "PWA" mode or in "Native" mode ?
The Recaptcha may be mandatory depending on how your app is distributed.
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
);
I'm not sure about your implementation of the appcheck-debug dependency on the Android Native side.
Since it's already implemented by the Flutter library itself, you should remove it.
Personal note : I had troubles with FirebaseAppCheck on some devices, while it was working perfectly on other devices.
This library is still in beta and I would recommend to wait before using it in production.
I'm using the Geolocator version 7.0.3 flutter package in my project and wanted to listen for the service status changes. In the documentation they are stating:
To listen for service status changes you can call the getServiceStatusStream. This will return a Stream which can be listened to, to receive location service status updates.
import 'package:geolocator/geolocator.dart';
StreamSubscription<ServiceStatus> serviceStatusStream = Geolocator.getServiceStatusStream().listen(
(ServiceStatus status) {
print(status);
});
But when calling Geolocator.getServiceStatusStream(); this error is shown:
The method 'getServiceStatusStream' isn't defined for the type 'Geolocator'.
Try correcting the name to the name of an existing method, or defining a method named 'getServiceStatusStream'.
Even in the breaking changes in 7.0.0
there is nothing saying that they moved it somewhere else or removed it.
Make sure to use version 7.1.0 or higher
I'm trying to run tests to ensure that my flutter database works as intended however when ran I get the following error:
package:flutter/src/services/platform_channel.dart 319:7 MethodChannel.invokeMethod
MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)
The app itself can access getApplicationDocumentsDirectory but not the test iself.
Here is my test suit:
import 'package:ema/DatabaseHelper.dart';
import 'package:ema/Readings.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart';
import 'package:ema/main.dart';
void main() {
test("Tests to ensure that the database is working as intended ", () async {
runApp(MyApp());
//first checks to see if the database is generated and is empty
List<Reading> dbResults = await DatabaseHelper.db.getReadings();
expect(0, dbResults.length);
});
}
dear, I think you should:
- stop the execution of your application
- re-run it
hot reload works only on dart code (lib)
Anyone know how to ping google and get latency in Flutter? I am using dart_ping but fail to ping.
Try dart_ping:
Setup:
dependencies:
dart_ping: ^1.0.0
Notes:
Watch the spacing. It should be excatly correct in the pubspec.yaml . You can add the second line (dart_ping: ^1.0.0) right below cupertino_icons: ^....
Remember to click on Packages Get (in Android Studio).
Usage:
import 'package:dart_ping/dart_ping.dart';
main() async {
var stream = await ping("google.com", times: 5);
print("Pinging google.com");
stream.listen((d) {
print(d.time.inMilliseconds);
});
}