Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : Bad state: Stream has already been listened to

Tags:

flutter

dart

     class MyPage extends StatelessWidget {       @override       Widget build(BuildContext context) {         return DefaultTabController(           length: 2,           child: new Scaffold(             appBar: TabBar(               tabs: [                 Tab(child: Text("MY INFORMATION",style: TextStyle(color: Colors.black54),)),                 Tab(child: Text("WEB CALENDER",style: TextStyle(color: Colors.black54),)),               ],             ),             body:PersonalInformationBlocProvider(               movieBloc: PersonalInformationBloc(),               child: TabBarView(                 children: [                   MyInformation(),                   new SmallCalendarExample(),                 ],               ),             ),           ),         );       }     }          class MyInformation extends StatelessWidget{       // TODO: implement build       var deviceSize;            //Column1       Widget profileColumn(PersonalInformation snapshot) => Container(         height: deviceSize.height * 0.24,         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             Row(               mainAxisAlignment: MainAxisAlignment.spaceEvenly,               children: <Widget>[                 Container(                   decoration: BoxDecoration(                     borderRadius:                     new BorderRadius.all(new Radius.circular(50.0)),                     border: new Border.all(                       color: Colors.black,                       width: 4.0,                     ),                   ),                   child: CircleAvatar(                     backgroundImage: NetworkImage(                         "http://www.binaythapa.com.np/img/me.jpg"),                     foregroundColor: Colors.white,                     backgroundColor: Colors.white,                     radius: 40.0,                   ),                 ),                 ProfileTile(                   title: snapshot.firstName,                   subtitle: "Developer",                 ),                 SizedBox(                   height: 10.0,                 ),               ],             )           ],         ),       );       Widget bodyData(PersonalInformation snapshot) {         return SingleChildScrollView(             child: Column(               children: <Widget>[                 profileColumn(snapshot)               ],             ),         );       }                 @override       Widget build(BuildContext context) {         final personalInformationBloc = PersonalInformationBlocProvider.of(context);              deviceSize = MediaQuery.of(context).size;         return StreamBuilder(             stream: personalInformationBloc.results,             builder: (context,snapshot){               if (!snapshot.hasData)                 return Center(                   child: CircularProgressIndicator(),                 );               return bodyData(snapshot.data);             }         );       }     }      

I am using Bloc Pattern for retrieving data from Rest API (just called the whole object from JSON and parsed user name only). The Page consists of two tabs MyInformation and SmallCalendar. When the app runs the data are fetched correctly and everything is good. When I go to tab two and return to tab one then the whole screens in tab one goes to red showing error: Bad state: Stream has already been listened to.

like image 843
BINAY THAPA MAGAR Avatar asked Jul 18 '18 08:07

BINAY THAPA MAGAR


People also ask

What is StreamController flutter?

StreamController<T> class Null safety. A controller with the stream it controls. This controller allows sending data, error and done events on its stream. This class can be used to create a simple stream that others can listen on, and to push events to that stream.

How do you use StreamSubscription in flutter?

A subscription on events from a Stream. When you listen on a Stream using Stream. listen, a StreamSubscription object is returned. The subscription provides events to the listener, and holds the callbacks used to handle the events.

What is RX Dart?

RxDart is an implementation of the popular reactiveX api for asynchronous programming, leveraging the native Dart Streams api.


1 Answers

You should use the following.

StreamController<...> _controller = StreamController<...>.broadcast(); 
like image 59
hrsma2i Avatar answered Dec 03 '22 08:12

hrsma2i