tencentcloud_cls_sdk_dart_producer 0.0.4
tencentcloud_cls_sdk_dart_producer: ^0.0.4 copied to clipboard
tencent cloud cls dart sdk.
tencentcloud_cls_sdk_dart_producer #
get start #
Depend on it Run this command:
With Dart:
dart pub add tencentcloud_cls_sdk_dart_producer
With Flutter:
flutter pub add tencentcloud_cls_sdk_dart_producer
This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):
dependencies:
tencentcloud_cls_sdk_dart_producer: ^0.0.1
Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.
Example #
import 'package:fixnum/src/int64.dart';
import 'package:flutter/material.dart';
import 'package:tencentcloud_cls_sdk_dart_producer/tencentcloud_cls_sdk_dart_producer.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 {
var config = LogProducerConfig();
config.endpoint = "ap-guangzhou-open.cls.tencentcs.com";
config.topicId = "[日志主题ID]";
config.secretId = "[SecretId]";
config.secretKey = "[SecretKey]";
_logProducer = LogProducer(config);
print('init producer client success');
}
void _callback() async {
if (!check()) {
return;
}
_logProducer?.setLogCallback((topicId, requestId, status, errorMessage) => print('$topicId, $requestId, $status, $errorMessage'));
print('init callback success');
}
void _sendLog() async {
if (!check()) {
return;
}
List<Log> logs = <Log>[];
Log log = Log();
log.contents.add(Log_Content()
..key = "hello"
..value = 'world');
log.time = Int64(DateTime
.now()
.millisecondsSinceEpoch) as Int64;
logs.add(log);
_logProducer?.send(logs);
}
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),
)),
)),
],
);
}
}