Heri
Heri

Reputation: 1

Error The argument type 'Object?' can't be assigned to the parameter type 'Color?'

There is my code : The StreamBuilder has an error

Error The argument type 'Object?' can't be assigned to the parameter type 'Color?

body: Center(
          child: StreamBuilder(
            stream: bloc.stateStream,
            initialData: Colors.amber,
            builder: (context, snapshot) {
              return AnimatedContainer(
                duration: Duration(milliseconds: 500),
                width: 100,
                height: 100,
                color: snapshot.data,
              );
            },
          ),
        ),
        body: Center(
          child: StreamBuilder(
            stream: bloc.stateStream,
            initialData: Colors.amber,
            builder: (context, snapshot) {
              return AnimatedContainer(
                duration: Duration(milliseconds: 500),
                width: 100,
                height: 100,
                color: snapshot.data,
              );
            },
          ),
        ),
      ),
    );
  },
}

Upvotes: 0

Views: 865

Answers (1)

Victor Eronmosele
Victor Eronmosele

Reputation: 7686

Your getting the error because the snapshot is of type AsyncSnapshot<Object?> and when you set the color to snapshot.data, the type Object, there's a type mismatch since the color should be of type Color.

You can fix the error by specifying the type of stream your StreamBuilder is using like below:

    body: Center(
              child: StreamBuilder<Color>( //Add <Color> after StreamBuilder
                stream: bloc.stateStream,
                initialData: Colors.amber,
                builder: (context, snapshot) {
                  return AnimatedContainer(
                    duration: Duration(milliseconds: 500),
                    width: 100,
                    height: 100,
                    color: snapshot.data,
                  );
                },
              ),
    ),

Upvotes: 1

Related Questions