-1

I have a list of 'Foo' and later got some periodic single Foo updates. Can I update the existing list?

Sank
  • 1
  • 2

1 Answers1

0

Disclaimer: I am not a Flutter nor Dart user.

  • I assume that your Foo and UpdatedFoo types have some kind of primary key member, such as FooId.
  • I assume you're able to maintain an in-memory Map<K,V> of Foo instances by their FooId value.
  • I assume you're not using the React, Redux, or Flux patterns.

Something like this:

class FooStore
{
    private Map<int,Foo> foos = new Map<int,Foo>();

    public void handleInitialFooStream( Stream<List<Foo>> initialStream ) async
    {
        // This `for` loop should only iterate once, btw.
        await for( List<Foo> fooList in initialStream )
        {
            this.setInitialFoos( fooList );
        }
    }

    private void setInitialFoos( List<Foo> initialFoos )
    {
        foreach( Foo foo in initialFoos )
        {
            this.foos[ foo.FooId ] = foo;
        }
    }

    public void handleFooStream( Stream<Foo> updateStream ) async
    {
        await for( Foo foo in updateStream )
        {
            this.foos[ foo.FooId ] = foo;
        }
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374