handy_tdlib 2.3.10
handy_tdlib: ^2.3.10 copied to clipboard
TDLib for Dart. Uses libtdjson. Improved fork of tdlib package by i-Naji
Handy TDLib #
This plugin is an updated fork of i-Naji's TDLib plugin with Dart 3 features and new TDLib version
About plugin #
This is a Flutter plugin for TDLib (Telegram Database Library) v1.8.36
This plugin is a complete TDLib's JSON interface binding package to help you create your own Telegram client.
Installation #
Install the plugin by running the following command from the project root:
flutter pub add handy_tdlib
Initialization #
This plugin supports only Android.
Consider running this plugin isolated from the main thread. Do convertJsonToObject()
on separate thread to avoid any freezes.
Setting up #
-
Set up 2 isolates with the working
SendPort+ReceivePortpair. -
Get TDLib client ID with
TdPlugin.instance.tdCreateClientId()on invokes thread. -
Transfer this ID to updates thread.
-
Set up
await foron invokes thread forReceivePort.Run
TdPlugin.instance.tdSendon every event fromReceivePort. This will send all updates to TDLib. Consider running jsonEncode on this thread to improve UI performance.On the main thread, you should use `SendPort' with TdFunction to send invokes.
Hey, what about
extraargument inTdFunction.toJson?You should put here any random number and save it somewhere in your class instance. This is used as a sign to detect what invoke's result have you got from updates thread. Consider using
Timerto make timeouts for invokes. -
On updates thread, set up
while (true) { ... }with something like thisfinal String? response = TdPlugin.instance.tdReceive(clientId); if (response != null) { try { sendPort.send(convertJsonToObject(response)); } catch (e) { ... some logging of this exception ... } }On UI thread, you'll get updates and invoke results from ReceivePort of this isolate.
Questions? #
Hey, but how we'll separate invoke results from updates?
// Only invoke results have 'extra' field.
final bool isUpdate = object.extra == null;
Library uses sealed classes, so you can use Dart 3's new switch (...) { ... } on object type if needed.
Consider importing lib/api.dart with prefix (import '...' as ...). Some object names are the same with dart:io and Flutter.
Still have questions? #
Feel free to PM me in Telegram.
Check HandyGram on GitHub for some code examples.
Usage #
- TDLib Data Repository layer in HandyGram
- Documentation button on this pub page
- Official TDLib guide (a bit old)