HomeMobile DevelopmentImplement Websocket on flutter App - Binance API with source code Implement Websocket on flutter App - Binance API with source code Makarablue August 23, 2022 0 Package :flutter pub add web_socket_channelSource Code :import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:web_socket_channel/io.dart'; void main() { runApp(const MaterialApp( debugShowCheckedModeBanner: false, home: WebsocketDemo())); } class WebsocketDemo extends StatefulWidget { const WebsocketDemo({Key? key}) : super(key: key); @override State createState() => _WebsocketDemoState(); } class _WebsocketDemoState extends State { String btcUsdtPrice = "0"; final channel = IOWebSocketChannel.connect( 'wss://stream.binance.com:9443/ws/btcusdt@trade'); @override void initState() { super.initState(); streamListener(); } streamListener() { channel.stream.listen((message) { // channel.sink.add('received!'); // channel.sink.close(status.goingAway); Map getData = jsonDecode(message); setState(() { btcUsdtPrice = getData['p']; }); // print(getData['p']); }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.blueAccent, body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "BTC/USDT Price", style: TextStyle( fontWeight: FontWeight.bold, color: Colors.white, fontSize: 30), ), Padding( padding: const EdgeInsets.all(8.0), child: Text( btcUsdtPrice, style: TextStyle( fontWeight: FontWeight.bold, color: Color.fromARGB(255, 250, 194, 25), fontSize: 30), ), ), ], ), ), ); } } Video : Tags Android App Coding Flutter Mobile Development Newer Older