skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. Bool represents a native bool in C.

    In order to use c libraries in a dart program we have to use ffi(Foreign Function Interface which is a module in dart.

    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 in C which returns int value. If in case you have a function which returns bool you can mention ffi.Bool Function().

    random_number.h

    int fetch_number();
    

    random_number.c

    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    #include "random_number.h"
    
    int main(){
        printf("%d", fetch_number());
        return 0;
    }
    
    int fetch_number(){
        srand(time(0));
        return rand() % 100 + 1;
    }
    

    main.dart

    import "dart:ffi" as ffi;
    import 'dart:io' show Platform;
    import 'package:path/path.dart' as path;
    
    void main(final List<String> args) {
      test();
    }
    
    
    void test() {
      final String dir =
          "/home/devpa/Documents/code/testing/testing_dart/lib/clibs";
    
      var libraryPath = path.join(dir, 'librandom_library.so');
    
      if (Platform.isMacOS) {
        libraryPath = path.join(dir, 'librandom_library.dylib');
      } else if (Platform.isWindows) {
        libraryPath = path.join(dir, 'Debug', 'librandom_library.dll');
      }
    
      final dylib = ffi.DynamicLibrary.open(libraryPath); // loading the library
      final int Function() getRandomNumber = dylib
          .lookup<ffi.NativeFunction<ffi.Int32 Function()>>('fetch_number')
          .asFunction(); // fetch_number is the method name
    
      print(getRandomNumber()); // will generate and display a random number
    }
    

    Here you will find more about ffi module in dart and also some example programs and there is a Makefile which you can use to generate libraries for various os.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search