zex_rectooor
zex_rectooor

Reputation: 992

Create stream builder

Get this errors:

1.A value of type 'Stream' can't be returned from function 'stream' because it has a return type of 'Stream'. (return_of_invalid_type at blahblah)

2.The argument type 'Stream' can't be assigned to the parameter type 'Stream'. (argument_type_not_assignable at blahblah)

why? here is code to create stream builder base on this video from flutter team


void main() {
  runApp(MyApp());

  //listen to subscribe to stream
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Center(
          child: Stream(),
        ),
      ),
    );
  }
}

class Stream extends StatefulWidget {
  @override
  _StreamState createState() => _StreamState();
}

class _StreamState extends State<Stream> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      //Error number 2
      stream: NumberCreator().stream,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else if (snapshot.connectionState == ConnectionState.done) {
          return Text('done');
        } else if (snapshot.hasError) {
          return Text('Error!');
        } else {
          return Text(snapshot.data);
        }
      },
    );
  }
}

class NumberCreator {
  NumberCreator() {
    Timer.periodic(Duration(seconds: 1), (timer) {
      //add count to stream
      _controller.sink.add(_count);
      _count++;
    });
  }
  var _count = 1;
  final _controller = StreamController<int>();
  
  //error number 1
  Stream<int> get stream => _controller.stream;

  dispose() {
    _controller.close();
  }
}

and Do we need statefulWidget to create stramBuilder?

Upvotes: 0

Views: 164

Answers (1)

Akshay Kumar U
Akshay Kumar U

Reputation: 436

The name of your widget class is causing problem because

class Stream extends StatefulWidget

is causing conflict with the name used by StreamBuildWidget parameter

stream: NumberCreator().stream,

So Try Changing your widgetName to something like StreamPage etc.

and also change

return Text(snapshot.data);

to

return Text(snapshot.data.toString());

even this is throwing error Text widget expects the String not int

Corrected Version of code

class StreamBuilderPage extends StatefulWidget {
  @override
  _StreamBuilderPageState createState() => _StreamBuilderPageState();
}

class _StreamBuilderPageState extends State<StreamBuilderPage> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      //Error number 2
      stream: NumberCreator().stream,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else if (snapshot.connectionState == ConnectionState.done) {
          return Text('done');
        } else if (snapshot.hasError) {
          return Text('Error!');
        } else {
          return Text(snapshot.data.toString());
        }
      },
    );
  }
}

Upvotes: 1

Related Questions