Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

Flutter StreamProvider is not subtype

I've been trying to get this to work, but with no success. I get this error: type '_HandleErrorStream<dynamic>' is not a subtype of type 'Stream<String>' This seems to happen when i try to add the websocket to the streamController. I dont know that is the right way to do it.

class FoodProvider {    
    List<String> comments;
    IOWebSocketChannel webSocketChannel;
    StreamController<String> streamController = StreamController<String>.broadcast();
    Stream<String> get stream => streamController.stream;

    FoodProvider() {
        foodName = <String>[];
        webSocketChannel = IOWebSocketChannel.connect("ws://myip:8080");
        streamController = StreamController.broadcast();
        streamController.addStream(webSocketChannel.stream);
    }
    
    void dispose() {
        webSocketChannel.sink.close();
        streamController.close();
    }
}

StreamProvider<String>.value(
    value: FoodProvider().stream,
    initialData: null,
    catchError: (_, __) => null,
    child: const FoodWidget(),
),

class Test extends StatelessWidget {
    const Test();
    @override
    Widget build(BuildContext context) {
        return child: Center(
            child: Consumer<String>(
                builder: (context, String food, child) {
                    return Column(
                        children: <Widget>[
                            Text("Food Name: $food"),
                        ],
                    );
                },
            ),
        );
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...