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.
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 ~ %