I have a list of 'Foo' and later got some periodic single Foo updates. Can I update the existing list?
Asked
Active
Viewed 71 times
1 Answers
0
Disclaimer: I am not a Flutter nor Dart user.
- I assume that your
Foo
andUpdatedFoo
types have some kind of primary key member, such asFooId
. - I assume you're able to maintain an in-memory
Map<K,V>
ofFoo
instances by theirFooId
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
>` instead of `Stream?`? What does the `List` represent exactly?
– Dai Mar 03 '21 at 22:52