Search code examples
fluttersqlitedartmobxdrift

ObservableStream<List<Type>> convert to List and/or ObservableList<Type> Flutter Mobx


I have setup a sqlite stream from Drift: Stream<List> into my @oberservable class. I can make it an OberserableStream, but I dont understand how to get the list from the stream.

My question is how can I get the list from the ObservableStream and make it an ObservableList or make it an ObeservableList directly from Stream?

Here's what I got wrong so far. I have tried many permutations of this but IDE keeps telling me that its wrong type, wrong .map, wrong <whatever>

@observable  
ObservableList<TodoDrift> streamTodos() {
    ObservableStream<List<TodoDrift>> list = stObv.asObservable();
    List<TodoDrift> prod = list.map(
            (e) {
               return e.done != true;
              });
    return prod.asObservable();
    // return Future.wait(olStream);
  }

Thank you!

FWIW. I also have a Future<List> from Drift and that works fine, I can get the list from there, so that works and I know the data is there.


Solution

  • answering your original question: this is how you could do that:

    ObservableList<int> streamToObservableList(Stream<List<int>> stream) {
      final list = ObservableList<int>();
      stream.listen((value) => list..clear()..addAll(value));
      return list;
    }
    

    BUT, this is really useless to do that since you already have ObservableStream which you can directly use inside Observer widget:

    ObservableStream<List<int>> stream = ...
    
    @override
    Widget build(BuildContext context) {
      return Observer(
        builder: (_) {
          List<int>? value = stream.value;
          print('stream value: $value');
          // use `value` for building your widget tree
          return ...;
        },
      );
    }