import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
class HomePage extends StatefulWidget {
final WebSocketChannel channel;
HomePage({Key key, @required this.channel}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Map<String, dynamic> request = {
"id": 1,
"jsonrpc": "2.0",
"method": "getCortexInfo"
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test 1'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: IconButton(
icon: Icon(Icons.send),
onPressed: () {
widget.channel.sink.add(jsonEncode(request));
}),
),
StreamBuilder(
stream: widget.channel.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
print('Response: ${snapshot.data}');
print('Error: ${snapshot.error}');
return Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text(
snapshot.hasData ? '${snapshot.data}' : 'Not response'),
);
}),
],
));
}
@override
void dispose() {
widget.channel.sink.close();
super.dispose();
}
}