Force Type

A Dart package that provides functions to forcefully convert dynamic types to specific types.

pub package pub package

Installation

Add the following dependency to your pubspec.yaml file:

dependencies:
  force_type: any

Usage

Import the force_type package and the test package:

import 'package:force_type/force_type.dart';
import 'package:test/test.dart';

Create a group of tests using the group function from the test package:

void main() {
  group('A group of tests', () {
    final dynamicA = 123;
    final dynamicB = "123";
    final dynamicC = "123.456";
    final dynamicD = "true";
    final dynamicE = "1";
    final dynamicF = "1969-07-20 20:18:04Z";

    test('Force String Test', () {
      expect(dynamicA.forceString, "123");
      //or
      expect(forceString(dynamicA), "123");
    });

    test('Force Integer Test', () {
      expect(dynamicB.forceInteger, 123);
      //or
      expect(forceInteger(dynamicB), 123);
    });

    test('Force Double Test', () {
      expect(dynamicC.forceDouble, 123.456);
      //or
      expect(forceDouble(dynamicC), 123.456);
    });

    test('Force Boolean Test', () {
      expect(dynamicD.forceBoolean, true);
      expect(dynamicE.forceBoolean, true);
      //or
      expect(forceBoolean(dynamicD), true);
      expect(forceBoolean(dynamicE), true);
    });

    test('Force DateTime Test', () {
      expect(dynamicF.forceDateTime, DateTime.parse("1969-07-20 20:18:04Z"));
      //or
      expect(forceDateTime(dynamicF), DateTime.parse("1969-07-20 20:18:04Z"));
    });
  });
}

API Reference

forceString

Converts a dynamic value to a String.

dynamicValue.forceString

forceInteger

Converts a dynamic value to an integer.

dynamicValue.forceInteger
//or
forceInteger(dynamicValue)

forceDouble

Converts a dynamic value to a double.

dynamicValue.forceDouble
//or
forceDouble(dynamicValue)

forceBoolean

Converts a dynamic value to a boolean.

dynamicValue.forceBoolean
//or
forceBoolean(dynamicValue)

forceDateTime

Converts a dynamic value to a DateTime object.

dynamicValue.forceDateTime
//or
forceDateTime(dynamicValue)

That's it! You can now use the force_type package to forcefully convert dynamic types to specific types in your Flutter project.

I hope this documentation helps! Let me know if you need any further assistance.

Libraries

force_type