Search code examples
firebaseflutterprovider

Flutter StreamProvider<List<DogModel>> can't be assigned


I'm getting the following error The argument type 'StreamProvider<List<DogModel>>' can't be assigned to the parameter type 'Stream<List<DogModel>>?'

Happing when I try to set the stream stream: stream,

Here the stream in the widget state

final stream = StreamProvider<List<DogModel>>(
create: (context) =>
    DogFirestoreService().getDogs('GeVnAbdq9BWs1STbytlAU65qkbc2'),
initialData: const [],

);

And the widget build

Widget build(BuildContext context) {
//final test = context.watch<List<DogModel>>();
//print(test);
return SizedBox(
  width: MediaQuery.of(context).size.width,
  child: StreamBuilder<List<DogModel>>(
    stream: stream,
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return const Text('no data');
      }

      if (snapshot.hasError) {
        return const Text('error');
      }

      if (snapshot.hasData) {
        return SizedBox(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.only(
                  top: 65.0,
                  left: 18.0,
                  right: 18.0,
                ),
                child: Text(
                  dogsCount(value.dogsList.length),
                  style: AppStyles.listsHeaderTextStyle,
                ),
              ),
              ListView.builder(
                padding: const EdgeInsets.only(
                  top: 5,
                  bottom: 20,
                  left: 15,
                  right: 15,
                ),
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: value.dogsList.length,
                itemBuilder: (context, index) => DogProfileCardWidget(
                  dogInfo: value.dogsList[index],
                  editProfile: openProfileScreen,
                  dogIndex: index,
                ),
              ),
            ],
          ),
        );
      }
      return const Text('data');
    },
  ),
);

} }


Solution

  • Your problem is that the stream variable is a StreamProvider.

    StreamProviders are not Streams, they are Widgets. So StreamBuilder does not understand what it is.

    Instead you may want to do:

    
    class Example extends StatefulWidget {
      const Example({Key? key}) : super(key: key);
    
      @override
      _ExampleState createState() => _ExampleState();
    }
    
    class _ExampleState extends State<Example> {
      // The stream needs to be stored in a StatefulWidfet
      // as you shouldn't create a new stream when the widget rebuild
      final stream = DogFirestoreService().getDogs('GeVnAbdq9BWs1STbytlAU65qkbc2');
    
      @override
      Widget build(BuildContext context) {
        return StreamBuilder<List<Dog>>(
          stream: stream,
          builder: ...,
        );
      }
    }