Search packages, repositories and app categories

Package detailsv1.0.0

adaptive_number

Library providing an adaptive number implementation. On JS runtimes, a 64-bit signed fixed-width integer will be used and for all other platforms the default Dart int data type.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
702K
Likes
2
Pub points
150

Install package

flutter pub add adaptive_number

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The adaptive_number package provides a number type that adapts to the runtime environment. On JavaScript platforms, it uses a 64-bit signed fixed-width integer for consistent precision, while on all other platforms it defaults to Dart's native int. This ensures reliable behavior across web and native environments, particularly when handling large integers or requiring cross-platform consistency in numerical operations.

Use cases

  • Cross-platform integer handling
  • Web-based applications with large numbers
  • Consistent arithmetic across platforms
  • Data serialization involving integers
  • Numerical computations in Flutter web apps

Key features

  • Adaptive integer size based on platform
  • 64-bit fixed-width integers on JS
  • Native int fallback on non-JS platforms
  • Consistent behavior across runtimes
  • Simple integration with existing code

Suitable for

  • Developers targeting web and native platforms
  • Apps requiring precise integer arithmetic
  • Projects needing portability of numeric logic
  • Teams avoiding JS-specific number limitations
  • Flutter apps with shared business logic

Considerations

  • Limited to integer values only
  • No support for floating-point numbers
  • Performance overhead on non-JS platforms
  • May not be needed for simple use cases
  • Requires careful testing on JS targets

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

adaptive_number

pub package Language Build codecov

A library providing a Number implementation that is dependent on the platform it is compiled for. For JS runtimes, the implementation uses a 64-bit signed fixed-width integer, for all other platforms the default Dart int data type.

Use cases of this library

The Number substitute is useful in situations where a value may overflow on JS runtimes since the available value range is not the same compared to mobile or desktop platforms:

https://api.dart.dev/stable/2.8.4/dart-core/int-class.html

The overflow on JS environments can be circumvented by using a BigInt or the fixed-with implementation of this library: https://pub.dev/packages/fixnum

The disadvantage of just replacing all usages of int with BigInt or fixnum/Int64 is that the performance on mobile platforms may suffer significantly.

The adpative_number library takes a conditional import approach to ease this problem:

  • If it runs on a JS runtime, Number wraps a fixnum/Int64
  • For all other platforms Number wraps a default Dart int type.

Some historical background of this library can also be seen in: https://github.com/MixinNetwork/libsignal_protocol_dart/pull/43

Usage

A Number instance is created by wrapping it around a default int value:

import 'package:adaptive_number/adaptive_number.dart';

void main() {
  final number1 = Number(93432);
  final number2 = Number(42435);
  assert((number1 + number2) == Number(51777));
}

All other operations on Number behave the same way as with int.

Features and bugs

Please file feature requests and bugs at the issue tracker.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package