2023-07-04 15:58:32 +08:00

61 lines
1.6 KiB
Dart

import 'package:cim_flutter_sdk/cim_socket.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String Message = 'Unknown';
@override
void initState() {
super.initState();
initCimState();
}
final CIMSocket cimSocket = CIMSocket();
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initCimState() async {
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
cimSocket.init('api.hoxin.farsunset.com', 34567, 16501516154949);
cimSocket.connect();
cimSocket.addListener(() {
if (cimSocket.model == null) return;
setState(() {
Message = cimSocket.model!.toProto3Json().toString();
});
});
} on PlatformException {}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Message is: $Message\n'),
),
),
);
}
}