Reputation: 1
Language : Dart(Flutter)
Problem : Unable to get value to Streambuilder
Hello, Real-time data received via restapi through a web socket. And I wanted to show the received data in real time on the app screen I used Streambuilder and the snapshot went null. What should I do? Advice is welcome.
// home_screen.dart (Files to Run)
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:kis/services/domestic_fo/real_time/future_hoka.dart';
class HomeScreen extends StatelessWidget {
HomeScreen({super.key});
Stream realfuture = realFutureHoka();
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: realfuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('${snapshot.data}');
// I want is this line
}
return Center(child: CircularProgressIndicator());
// but result this line
},
),
backgroundColor: Colors.white,
);
}
}
i can receive the value through the WebSocket channel.print result type is String No problem until streambuilder was used
// future_hoka.dart (Data Files Received by WebSocket)
Stream realFutureHoka() async* {
final channel = WebSocketChannel.connect(
Uri.parse("ws://ops.koreainvestment.com:21000/tryitout/H0IFASP0"));
await channel.ready;
channel.sink.add(
jsonEncode(
{
"header": {
"appkey": appkey,
"appsecret": appsecret,
'custtype': 'P',
'tr_type': '1',
'content-type': 'utf-8'
},
"body": {
"input": {"tr_id": "H0IFASP0", "tr_key": FUTURE_CODE}
}
},
),
);
channel.stream.listen(
(data) {
print(data);
return data;
},
);
}
Upvotes: 0
Views: 56