tencent cloud

Flutter SDK Log Upload
Last updated: 2025-11-07 17:40:39
Flutter SDK Log Upload
Last updated: 2025-11-07 17:40:39
This article introduces how to quickly get started with the Flutter SDK of Cloud Log Service (CLS) to implement log upload.

Prerequisites

Create and obtain TencentCloud API key information accessKey and accessSecret. For key information acquisition, please visit API Key Management.
Please ensure the associated account has appropriate SDK log upload permission.
Installed Flutter environment. For more information, see Install Flutter.

Installing the Flutter SDK

1. Create a Flutter project.
2. Execute the following command in the root directory of the project to add dependencies.
flutter pub add tencentcloud_cls_sdk_dart
3. After installation, import the CLS module in your Dart file.
import 'package:tencentcloud_cls_sdk_dart/tencentcloud_cls_sdk_dart.dart';

Request Parameters

Variable
Type
Required
Description
host
String
Yes
Regional information. Please refer to the domain name in the available region under the Log upload via API Tab.
accessKey
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
accessSecret
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
topicId
String
Yes
Log topic ID info.
accessToken
String
No
Temporary key token. If using a temporary key, you can fill it in.

Upload Example Code for Logs

In your Flutter project, you can use the following example code to implement log upload. The sample code is as follows.
Using cloud API keys (accessKey, accessSecret) in code as plaintext carries high risk. To ensure security, recommend using temporary key to authenticate.
import 'package:flutter/material.dart';
import 'package:tencentcloud_cls_sdk_dart/tencentcloud_cls_sdk_dart.dart';

Future<void> main() async {
await RustLib.init();

runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
LogProducer? _logProducer;
String _consoleText = '';


@override
void initState() {
super.initState();

if (!mounted) return;
print("application started.");
}

void print(String message) {
setState(() {
_consoleText += message;
_consoleText += '\\n';
});
}

void _initProducer() async {
_logProducer = LogProducer(
topicId: '',
accessKey: '',
accessSecret: '',
accessToken: '',
host: 'ap-XXXXXX.cls.tencentcs.com',
waitSendLogQueue: 1000, addLogQueue: 1000, lingerMs: 2000);

print('init producer client success');
}

void _callback() async {
if (!check()) {
return;
}
_logProducer?.setCallback(dartCallback: (topicId, requestId, status, errorMessage) => print('$requestId!, $status, $topicId, $errorMessage'));
print('init callback success');
}

void _sendLog() async {
if (!check()) {
return;
}
_logProducer?.addLog(log: { 'hello': 'world' });
}

bool check() {
if (null == _logProducer) {
print('you should init producer first.');
return false;
}
return true;
}

@override
Widget build(BuildContext context) {
Color color = Theme.of(context).primaryColor;

return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('tencent cloud cls flutter sdk demo')),
body: Column(
children: [
_buildConsoleText(),
_buildButton(color, 'init', _initProducer),
_buildButton(color, 'set callback', _callback),
_buildButton(color, 'send', _sendLog),
],
),
),
);
}

Widget _buildConsoleText() {
return Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(bottom: 18),
padding: const EdgeInsets.all(6),
height: 140,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 0.67,
),
color: Colors.black),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Text(
_consoleText,
style: const TextStyle(
color: Colors.white,
fontSize: 12,
letterSpacing: 2,
wordSpacing: 2,
fontFeatures: [FontFeature.tabularFigures()]),
),
),
))
]);
}

Widget _buildButton(Color color, String label, VoidCallback? onPressed) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextButton(
onPressed: onPressed,
style: ButtonStyle(
shape: WidgetStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
side: WidgetStateProperty.all(BorderSide(color: color, width: 0.67)),
backgroundColor: WidgetStateProperty.all(Colors.transparent),
padding:
WidgetStateProperty.all(const EdgeInsets.only(left: 12, top: 6, right: 12, bottom: 6))),
child: Text(
label,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w400, color: color),
)),
)),
],
);
}

}

Conclusions

Through these steps, you can quickly use the Flutter SDK of Tencent Cloud CLS to upload logs. If you encounter any issues, contact us to get help.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback