skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    based on @pskink question, Here is the working code.

    State (mobx):

    //pulling from drift .watch() sqlite
    @observable
      Stream<List<TodoDrift>> stObv = _db.streamTodos(); 
    

    Display:

      const TodoListView({super.key});
    
      @override
      Widget build(BuildContext context) {
        final list = TodoDriftList(); //access mobx state class/store
    
        return StreamBuilder(
            stream: list.stObv, //@observed stream  returned from drift .watch() query
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return ListView.builder(
                    itemCount: snapshot.data!.length,
                    shrinkWrap: true,
                    itemBuilder: (_, index) {
                      return Observer(
                          builder: (_) {
                            final item = snapshot.data![index];
                            return Text(item.description); //strictly for testing output
                          }
                      );
                    }
                );
              } else {
                return const CircularProgressIndicator();
              }
            }
        );
      }
    }
    

  2. 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 ...;
        },
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search