What I'm doing is to fetch cartoon list and show by GridView. Below code is fetching data
Future<void> _getWebtoonData() async {
var response;
if(_daysReceivedResponse[_pressedButtonDayIndex]){
response = _daysResponse[_pressedButtonDayIndex];
} else {
response= await http.get('https://comic.naver.com/webtoon/weekdayList.nhn?week='+_currentWebtoonAddress);
_daysReceivedResponse[_pressedButtonDayIndex] = true;
_daysResponse[_pressedButtonDayIndex] = response;
}
dom.Document document = parser.parse(response.body);
final e1 = document.querySelectorAll('.img_list .thumb');
final e2 = document.querySelectorAll('.img_list .desc');
final e3 = document.querySelectorAll('.img_list .rating_type');
List<List<String>> infoCollection = List<List<String>>();
List<String> info = List<String>();
for(int i=0; i<e1.length; i++){
info.add(e1[i].getElementsByTagName('img')[0].attributes['src']);
info.add(e1[i].getElementsByTagName('a')[0].attributes['title']);
info.add(e2[i].getElementsByTagName('a')[0].innerHtml);
info.add(e3[i].getElementsByTagName('strong')[0].innerHtml);
infoCollection.add(info);
}
_controller.sink.add(infoCollection);
}
And I'm showing this images, titles, artists and rate by GridView like below
Widget _getWebtoonGridView() {
return StreamBuilder(
stream: _controller.stream.asBroadcastStream(),
builder: (BuildContext context, AsyncSnapshot<List> snapshot){
if(snapshot.hasError)
print(snapshot.error);
else if(snapshot.hasData){
return GridView.count(
crossAxisCount: 3,
childAspectRatio: 0.6,
children: List.generate(snapshot.data.length, (index){
return _getWebtoonInfo(index, snapshot.data[index]);
}),
);
}
else if(snapshot.connectionState != ConnectionState.done)
return Center(child: CircularProgressIndicator());
},
);
}
But "Stream has already been listened to" error is occurring continuously, what is the problem about my StreamController??
How can I fix it?
StackTrace
I/flutter (21411): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (21411): The following StateError was thrown building Expanded(flex: 1):
I/flutter (21411): Bad state: Stream has already been listened to.
I/flutter (21411):
I/flutter (21411): When the exception was thrown, this was the stack:
I/flutter (21411): #4 _StreamBuilderBaseState._subscribe (package:flutter/src/widgets/async.dart:135:37)
I/flutter (21411): #5 _StreamBuilderBaseState.initState (package:flutter/src/widgets/async.dart:109:5)
I/flutter (21411): #6 StatefulElement._firstBuild(package:flutter/src/widgets/framework.dart:3830:58)
I/flutter (21411): #7 ComponentElement.mount(package:flutter/src/widgets/framework.dart:3696:5)
I/flutter (21411): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter (21411): #9 Element.updateChild(package:flutter/src/widgets/framework.dart:2753:12)
StreamController variable
StreamController<List<List<String>>> _controller = StreamController<List<List<String>>>.broadcast();