what is difference Bool(native bool) and bool in terms of using?
i got bool using getBool of SharedPreferences. and found out received type isn’t Bool(native bool).
for example…
abstract class PrefApi{
const PrefApi();
Bool getNativeBool(String key);
bool getBool(String key);
}
class PrefStorage extends PrefApi {
final SharedPreferences _plugin;
LocalStorageVariableApi({required SharedPreferences plugin}) : _plugin = plugin
// this is working!!
@override
bool getBool(String key) => _plugin.getBool(key)!;
// this isn't working!!
@override
Bool getNativeBool(String key) => _plugin.getBool(key)!;
i’m just wondering about difference in terms of using. When is Bool(native bool) used and When is bool used?
2
Answers
The main difference between Bool and bool is that Bool is not a native type in Dart, while bool is a native type. Bool is typically used as a wrapper class for boolean values in situations where nullability needs to be explicitly declared.
In your example, the getNativeBool method returns a Bool object, which is not the same as the native bool type returned by the getBool method. The getBool method returns a boolean value that is either true or false, while the getNativeBool method returns a Bool object that can be either true, false, or null.
If you need to work with nullable boolean values, then you can use the Bool wrapper class. However, if you only need to work with non-nullable boolean values, then you should use the native bool type. In most cases, you will probably want to use the native bool type for simplicity and performance reasons.
In order to use c libraries in a dart program we have to use
ffi
(Foreign Function Interface which is a module indart
.The
ffi
module provides various data types and methods through which we can interact with C program.In the below program we are creating libraries and using those libraries in our dart program.
Notice
ffi.Int32 Function()
it represents a function is there inC
which returnsint
value. If in case you have a function which returnsbool
you can mentionffi.Bool Function()
.random_number.h
random_number.c
main.dart
Here you will find more about
ffi
module in dart and also some example programs and there is aMakefile
which you can use to generate libraries for various os.